title: VBA code to pass parameter of ms-access form to query
body: <p>I can't seem to figure out a good way to code this into my VBA macro. I'm using Access. I have a form that's serving as a 'login' it Dlookups to make sure the user is within a user table. Once it's confirmed that the user is within the user table, I want it to run a query on another table with the criteria of the username. </p>
  
  <p>This is the criteria of my username field on my query referenced table:</p>
  
  <pre><code>[Forms]![LoginForm].[txtUserName]
  </code></pre>
  
  <p>This is the Macro of my login form click button</p>
  
  <pre><code>Private Sub btnLogin_Click()
  
      Dim User As String
      Dim UserLevel As Integer
      Dim TempPass As String
      Dim ID As Integer
      Dim UserName As String
      Dim TempID As String
  
  
      If IsNull(Me.txtUserName) Then
          MsgBox "Please enter UserName", vbInformation, "Username required"
          Me.txtUserName.SetFocus
      ElseIf IsNull(Me.txtPassword) Then
          MsgBox "Please enter Password", vbInformation, "Password required"
          Me.txtPassword.SetFocus
      Else
          If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" &amp; Me.txtUserName.Value &amp; "' And UserPassword = '" &amp; Me.txtPassword.Value &amp; "'"))) Then
              MsgBox "Invalid Username or Password!"
          Else
              TempID = Me.txtUserName.Value
              UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" &amp; Me.txtUserName.Value &amp; "'")
              UserLevel = DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" &amp; Me.txtUserName.Value &amp; "'")
              TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" &amp; Me.txtUserName.Value &amp; "'")
              UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" &amp; Me.txtUserName.Value &amp; "'")
              DoCmd.Close
              If (TempPass = "password") Then
                  MsgBox "Please change Password", vbInformation, "New password required"
                  DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " &amp; UserLogin
              Else
                  'open different form according to user level
                   DoCmd.OpenQuery "ClockDefectRun", , acReadOnly
              End If
          End If
      End If
  
  End Sub
  </code></pre>
  
  <p>Whenever I run this, My query opens, but it asks for the field as if it doesn't exist in my forms. How can I pass this programmatically?</p>