Results 1 to 3 of 3
  1. #1
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295

    How to use a variable in q query?


    Good Day,
    In my Vba query when a use a literal number 207 the expected results is obtained; however when I replace the 207 with a declared variable "Number" an error message "too few parameters. Expected 1" occurs.

    Can anyone explain this phenomenon?

    HTML Code:
    Public Sub SumSalesAccounts()
        Dim curDatabase As Database
        Dim rst As Recordset
        Dim strSql As String
        Dim Number As Integer
        
        Number = 207
        
        strSql = "SELECT Sum(Quantity) AS SumQty, Account, Sum([Extended Price]) AS SumPrice, code3 " _
    & "FROM [Chart of Accounts] INNER JOIN [Order Details Extended] ON [Chart of Accounts].[Account Name] = [Order Details Extended].Account " _
    & "WHERE [Order ID]= 207  GROUP BY Account, [Order ID], code3;"
      Debug.Print strSql
        
        Set curDatabase = CurrentDb
        Set rst = curDatabase.OpenRecordset(strSql)
        Do While (Not rst.EOF)
         MsgBox rst.Fields(0) & " " & rst.Fields(1) & " " & rst.Fields(2)
         rst.MoveNext
        Loop
      
       Set rst = Nothing
       Set curDatabase = Nothing
       
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    You need to reference a control on a form or a popup InputBox will allow user to enter a value.

    Number is a reserved word. Should not use reserved words as names for anything. Use something like intNum.

    Concatenate variables.

    & "WHERE [Order ID]= " & intNum & " GROUP BY Account, [Order ID], code3;"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Thanks. That was quick. It works fine.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 04-27-2015, 02:40 PM
  2. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  3. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  4. Replies: 4
    Last Post: 08-05-2010, 01:26 PM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums