Solved

variable in from clause

Posted on 2002-04-24
3
657 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
[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
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Updates not working for MS Windows 7 12 195
Add a task in Outlook from access 11 43
Macro Excel - Multiple If conditions 2 88
MS SQL Update query with connected table data 3 63
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

730 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