Discussion Locked

Visual Basic Record Set

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

In Visual Basic while assigning the value for a variable,I'm unable to pass the variable to the recordset Select (Sequel) statement.Shall I know the exact syntax and method to assign the variable instead of value.
Example: temp = 1070
data1.recordset=(shape{select * from tble where field=temp})

7 replies

by roc180

If you are passing a variable into your SQL statement you need to let VB know that you want to reference the value within the variable. For Example:

data1.recordset=(shape{select * from tble where field="& temp &"})

Notice "& temp &" by putting ("& &") around the variable it lets VB know you want the value held within temp. If temp was a string use '"& temp &"'.

Good Luck

Neal Martinelli

by burtsa

To send the temp variable to the select command you need to do 3 things.
First, I am assuming you are using ADO so the property to set under data1 is .RecordSource.
Second, RecordSource is a string so you need to surround the whole SQL statement with quotes.

Third, the you need to get temp int the SQL statement.

Here is how I do it.

Dim Temp
Dim SQL_Str
temp=1070
SQL_Str="data1.recordset=(shape{select * from tble where field=" & temp & "})"
data1.RecordSource=SQL_Str
data1.Refresh

Hope this helps

Shawn

by francodacosta

The SQL statment is a String, so you use String Contaction rulues and operators.

You pass the variable to the SQL statment the same way you put variables values in a string.

Just keep in mind that if the variable type is a string you must use quotation marks ('), or youll will get an run time error.

Then the correct code would be:
(assuming data1 is a datacontrol)

'**** For Numbers ****
data1.recordsource="(shape{SELECT * FROM table WHERE fieldname=" & temp &")}"
data1.refresh'**** For STRINGS ****
data1.recordsource="(shape{SELECT * FROM table WHERE fieldname='" & temp &"')}"
data1.refresh

'**** For DATES ****
data1.recordsource="(shape{SELECT * FROM table WHERE fieldname=#" & temp &"#)}"
data1.refresh

This discussion is closed to new replies.