Results 1 to 5 of 5
  1. #1
    markyboy171 is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    5

    Passing parameters into vb using access query

    I Have a basic select query using the access query builder
    In my VB of a form button i have the following

    Code:
    Private Sub Add_User_Click()
    On Error GoTo Err_Add_User_Click
    
    
        Dim Db As DAO.Database
        Dim tdf As DAO.TableDef
        Dim fld As DAO.Field
        Set Db = CurrentDb()
        Set tdf = Db.CreateTableDef("User_Table")
        Dim stDocName As String
        Dim strSQL As String
        Dim rs As DAO.Recordset
        Dim Counter As Integer
        Dim OP As String
        Dim Read As String
        Read = (Read)
        Dim OPP As QueryDef
        Set OPP = (Forms!Add_User_Manual![OPERATION])
        DoCmd.SelectObject (acQuery)
        stDocName = "Count"
        strSQL = "SELECT * FROM [stDocName] WHERE [Operation]=Read"
        Set rs = CurrentDb.OpenRecordset(strSQL)
        Counter = rs.RecordCount
        If (Counter > 0) Then
        MsgBox ("Success")
        Else
        MsgBox ("Fail")
        End If
        
    
    
    Exit_Add_User_Click:
        Exit Sub
    
    
    Err_Add_User_Click:
        MsgBox Err.DESCRIPTION
        Resume Exit_Add_User_Click
        
    End Sub
    Apologies please ignore all the rubbish ive been trying a lot of things.

    BTW The Count inside stDocName is the name of the query i have in the access



    The end goal of this is to get the parameter to be the string passed in from the text box.

    The Error i am getting when i click the button on the form is "Object required"
    Last edited by markyboy171; 06-14-2012 at 10:12 AM. Reason: Missing explanation

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,628
    Must concatenate variables, otherwise the variable name is just a literal string, also need apostrophe delimiters for text criteria:

    strSQL = "SELECT * FROM [" & stDocName & "] WHERE [Operation]='Read'"

    If you want parameter from textbox:
    stDocName = Me.textboxname
    or
    strSQL = "SELECT * FROM [" & Me.textboxname & "] WHERE [Operation]='Read'"
    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
    markyboy171 is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    5
    Thanks Very Much. Looks like that might work. I'll give it a go and see how I get on.

    Thanks Again

  4. #4
    markyboy171 is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    5
    Hi Im now recieving a different error

    Too few parameters. Expected 1

    Code:
        Dim Db As DAO.Database
    
    
        Set Db = CurrentDb()
    
    
        Dim stDocName As String
        Dim strSQL As String
        Dim rs As DAO.Recordset
        stDocName = "Count"
        strSQL = "SELECT * FROM [" & stDocName & "] WHERE [Operation]=[" & Me.OPERATION & "] "
        Set rs = CurrentDb.OpenRecordset(strSQL)
        Counter = rs.RecordCount
        If (Counter > 0) Then
        MsgBox ("Success")
        Else
        MsgBox ("Fail")
        End If
    This is my query "Count"
    Code:
    SELECT User_Table.OPERATION
    FROM User_Table;
    Just to Explain stDocName is my query in access Operation is the field the query is pulling and also the name of the textbox.

    Thanks

  5. #5
    markyboy171 is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2012
    Posts
    5
    For reference this is the fix
    Code:
    strSQL = "Select * FROM [" & stDocName & "] WHERE [Operation]='" & OperationP & "' AND [Object]= '" & ObjectP & "'"
    Where ObjectP and OperationP are strings based on the text boxes#

    Thanks for all the help

    Peace

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

Similar Threads

  1. Replies: 37
    Last Post: 09-15-2011, 11:57 AM
  2. Passing parameters from a form
    By rfs in forum Forms
    Replies: 1
    Last Post: 03-15-2011, 12:25 AM
  3. Passing parameters to a query
    By stephenaa5 in forum Queries
    Replies: 3
    Last Post: 09-10-2010, 05:32 PM
  4. Passing Parameters to a Data Access Page
    By stevie6410 in forum Access
    Replies: 0
    Last Post: 10-01-2009, 09:14 AM
  5. Passing parameters to a report
    By elmousa68 in forum Access
    Replies: 0
    Last Post: 12-07-2006, 01:38 AM

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