Results 1 to 4 of 4

Thread: I have probably asked already but trying again....(Access VBA)[Solved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved 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.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: I have probably asked already but trying again....(Access VBA)

    VB Code:
    1. Dim strSQL as String, strVariable as String
    2.  
    3. strSQL = "SELECT fldOne, fldTwo, fldThree " & _
    4. "FROM tblBesoup " & _
    5. "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

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    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
  •  



Click Here to Expand Forum to Full Width