|
-
Mar 7th, 2005, 10:21 AM
#1
Thread Starter
Frenzied Member
I have probably asked already but trying again....(Access VBA)[Solved]
Hey,
I have a table with 30 fields that I would like to query from using a variable as the criteria. Is this possible in Access VBA? I try to redefine the query but I get syntax errors because of the length of the SQL Statement.
Any input is appreciated...
Last edited by Besoup; Mar 7th, 2005 at 01:31 PM.
-
Mar 7th, 2005, 12:22 PM
#2
Frenzied Member
Re: I have probably asked already but trying again....(Access VBA)
VB Code:
Dim strSQL as String, strVariable as String
strSQL = "SELECT fldOne, fldTwo, fldThree " & _
"FROM tblBesoup " & _
"WHERE fldThree = '" & strVariable & "'"
There are single quotes surrounding strVariable after the = sign: - a single quote, double quote, space, ampersand, space, strVariable, space, ampersand, space, double quote, single quote, double quote.
You need the single quotes for strings, the # sign for dates, and nothing for numbers. Instead of strVariable, you could use txtTextBox.Text if the value was in a textbox control, for example, but I prefer using variables.
Tengo mas preguntas que contestas
-
Mar 7th, 2005, 12:53 PM
#3
Re: I have probably asked already but trying again....(Access VBA)
If the length of the sql statement is an issue, you can alais the table and field names to make it shorter.
VB Code:
SELECT ([FieldNameTest]) As F1, ([Someotherfieldname]) As F2 FROM MyTestingTable1 As T1 WHERE F1 = " & SomeVariable & ";"
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 7th, 2005, 01:31 PM
#4
Thread Starter
Frenzied Member
Re: I have probably asked already but trying again....(Access VBA)
Got it running using lots of & _, thanks for the help...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|