Solved

variable in from clause

Posted on 2002-04-24
3
658 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

690 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