Solved

variable in from clause

Posted on 2002-04-24
3
653 Views
Last Modified: 2012-05-04
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
Comment
Question by:mathprof
3 Comments
 
LVL 1

Accepted Solution

by:
Evlich earned 50 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

by:rspahitz
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

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now