How to FindRecords Properly in VBA instead of Macro

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SueHopson
    New Member
    • Jan 2020
    • 47

    How to FindRecords Properly in VBA instead of Macro

    So I have a lookup on a form that is searching for the Customer based on the following Embedded Macro

    Code:
    ="SearchForRecord
    ObjectType
    Object Name
    Record First
    Where Condition = [CustID] = " & Str(Nz([Screen].[ActiveControl],0))
    It's pulling on the following data:
    SELECT qry_cxLookup.Cu stID, qry_cxLookup.Co mpany FROM qry_cxLookup ORDER BY qry_cxLookup.Co mpany;

    The main form is using the same query, but is only displaying the record that matches an unbound text box on my main form.
    Code:
    txtCustID=[cmb_CxLookup].[Column](0)
    I added this to eliminate the form displaying the first record from table when it loads. The goal is to display the chosen company info only (fields are locked and disabled).

    So here is my problem, well problems, and (as usual) I'm positive I am making it more complicated than it has to be.
    1. I hate Macros for reason 2 and would love to convert this code to VBA (but the option is not available on the macro page)
    2. I can't figure out how on the AfterUpdate macro, to get the form to Requery/Refresh to update and display the records, which it does perfectly when I update manually.
    3. I need to either know how to hide the first company from displaying until the record is selected OR
    4. I need to be able to set the value from txtCustID to CustID AfterUpdate so that the company data displays automatically.


    Any help will be greeted with cheers and a big thank you!
  • SueHopson
    New Member
    • Jan 2020
    • 47

    #2
    Ok, so I was able to lookup the resources and create the VBA Code for the Search for Record successfully

    Code:
    Private Sub cmb_CxLookup_AfterUpdate()
        DoCmd.SearchForRecord acDataForm, "frmMain", acFirst, "[CustID] = " & str(Nz([Screen].[ActiveControl], 0))
        DoCmd.RefreshRecord
    End Sub
    The default view is Single Form not Continuous. and the form is pulled from the query - queCustomers. I'm still having the same problem on Item 3 above, which is the Customer Details displaying the first record of the query, despite nothing being selected in the Customer Lookup yet (cmb_CxLookup). The CUSTID field is a primary key so this may be unavoidable based on everything I have read/tried.

    These fields are all for show only (disabled and locked) and the user has to click an edit customer button to make any changes, so there's no danger of them accidentally overwriting that data. However, I've now also been asked if it's possible to filter the initial cmb_CxLookup.
    All Records - Active Only - Inactive Only

    I thought I might have found a viable solution with the code below, but I just can't it to filter the Customer Combo box - it does show all records.

    Code:
    Private Sub cmb_FilterStatus_Change()
        Dim sqlQry As String
        sqlQry = "SELECT * FROM queCustomers ORDER BY queCustomers.Company;"
        Select Case Me.cmb_FilterStatus
            Case 1 'All Records
                sqlQry = sqlQry
            Case 2 'Active Records Only
                sqlQry = sqlQry & " WHERE Active = True"
                    Case 2 'Active Records Only
            Case 2 'Inactive Records Only
                sqlQry = sqlQry & " WHERE Active = False"
            Case Else
                sqlQry = sqlQry
        End Select
        Me.cmb_CxLookup.RowSource = sqlQry
        Me.cmb_CxLookup.Requery
    End Sub
    Not sure what logic or step I am missing...

    Comment

    • SueHopson
      New Member
      • Jan 2020
      • 47

      #3
      While testing the individual code lines to see if I could find my mistake in my second post, I found a workaround I liked better. The buttons are displayed with no background or border and look/act like hyperlinks on the form - they look much cleaner and much more user-friendly.

      Code:
      Private Sub lnk_AllCustomers_Click()
          Me.cmb_CxLookup.RowSource = "SELECT * FROM queCustomers ORDER BY queCustomers.Company;"
          Me.cmb_CxLookup.Requery
      End Sub
      
      Private Sub lnk_Inactive_Click()
          Me.cmb_CxLookup.RowSource = "SELECT * FROM queCustomers WHERE Active = False ORDER BY queCustomers.Company;"
          Me.cmb_CxLookup.Requery
      End Sub
      
      Private Sub lnkActive_Click()
          Me.cmb_CxLookup.RowSource = "SELECT * FROM queCustomers WHERE Active = True ORDER BY queCustomers.Company;"
          Me.cmb_CxLookup.Requery
      End Sub
      I am still interested in knowing what I did wrong in the second port though.

      So I guess I've answered all my own questions except my original #3.
      Is there a way to hide the Customer Details until a selection is made from cmb_CxLookup?

      Comment

      Working...