Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# variable in from clause

Posted on 2002-04-24
Medium Priority
660 Views
I'm having trouble in VB6 with an access db using a variable (TableName\$) in the FROM clause of a SQL statement.

Set rs = dbmath.OpenRecordset("Select Password,Number, First, Last  FROM  TableName\$  where First = '" & TeacherFirst.Text & "' and Last = '" & TeacherLast.Text & " ' ")

What is the correct syntax?

Mathprof
0
Question by:mathprof
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 1

Accepted Solution

Evlich earned 200 total points
ID: 6967308
Try this...
Set rs = dbmath.OpenRecordset("Select Password,Number, First, Last  FROM "& TableName\$ &" where First =
'" & TeacherFirst.Text & "' and Last = '" & TeacherLast.Text & " ' ")
Hope that it helps.
~Evlich
0

LVL 22

Expert Comment

ID: 6967358
Evlich is correct.

If it helps in the future, you may want to break things down into smaller pieces:

' First define constants which can be carried from code to code or defined globally
Const cSelectVerb as string = "SELECT "
const cFromVerb as string = " FROM "
const cWhereVerb as string = " WHERE "

' Now define variable pieces:
Dim strSelectFieldList as string
dim strWhereClause as string

strSelectFieldList = "Password,Number, First, Last  "
strWhereClause = cWhereVerb & "First = '" & TeacherFirst.Text & "' and Last = '" & TeacherLast.Text & "'"

' Now assemble all the pieces:

Set rs = dbmath.OpenRecordset(cSelectVerb & strSelectFieldList & cFromVerb & TableName\$ & strWhereClause)

--
And if the where clause gets too complex, you could break that down in the same way as the select:

const cFieldName1 as string = " First "
const cFieldName2 as string = " Last "
const cAndVerb as string = " AND "
const cEquateVerb as string = "="

dim strFieldValue1 as string
dim strFieldValue2 as string

strFieldValue1 = "'" & TeacherFirst.Text & "'"
strFieldValue2 = "'" & TeacherLast.Text & "'"

strWhereClause = cWhereVerb & cFieldName1 & cEquateVerb & strFieldValue1 & cAndVerb & cFieldName2 & cEquateVerb & strFieldValue2

--
This may look confusing at first, but it's general-purpose enough that you could turn it into a function which assembled all the pieces for you and returns the correct SQL string.  That's a bit overkill for simply queries, but if you do a lot, it may be worthwhile.
0

Author Comment

ID: 6967391
Thanks you !. It worked.
And thank you too, RSPAHITZ, for helping out someone very new to SQL.
Mathprof
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month11 days, 1 hour left to enroll