If/Then help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KelHemp
    New Member
    • Oct 2006
    • 16

    If/Then help!

    I have a form that I want to automatically fill in information on.

    When the LeaseNumber field is equal to the PrevLeaseNumber field, I want to fill in LeaseName and Location with DLookup or some other action that would retrieve them from the previous record.

    Should I use a macro? SQL? An OnUpdate event?
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    In the After Update event of the LeaseNumber field control.

    Code:
     
    Public Sub LeaseNumber_AfterUpdate()
     
      If Me.LeaseNumber = Me.PrevLeaseNumber Then
    	Me.LeaseName = Dlookup("[LeaseName]","TableName","[LeaseNumber]=" & Me.LeaseNumber)
    	Me.Location = Dlookup("[Location]","TableName","[LeaseNumber]=" & Me.LeaseNumber)
      End If
     
    End Sub
    or if LeaseNumber is a text field then:

    Code:
     
    Public Sub LeaseNumber_AfterUpdate()
     
      If Me.LeaseNumber = Me.PrevLeaseNumber Then
    	Me.LeaseName = Dlookup("[LeaseName]","TableName","[LeaseNumber]='" & Me.LeaseNumber & "'")
    	Me.Location = Dlookup("[Location]","TableName","[LeaseNumber'" & Me.LeaseNumber & "'")
      End If
     
    End Sub

    Comment

    • KelHemp
      New Member
      • Oct 2006
      • 16

      #3
      Originally posted by mmccarthy
      In the After Update event of the LeaseNumber field control.

      Code:
       
      Public Sub LeaseNumber_AfterUpdate()
       
        If Me.LeaseNumber = Me.PrevLeaseNumber Then
      	Me.LeaseName = Dlookup("[LeaseName]","TableName","[LeaseNumber]=" & Me.LeaseNumber)
      	Me.Location = Dlookup("[Location]","TableName","[LeaseNumber]=" & Me.LeaseNumber)
        End If
       
      End Sub
      It is a number type. I went into the AfterUpdate and pasted the code, but nothing happened like I wanted. I hit the "Code Bulder" option and it automatically created the lines
      Code:
      Option Compare Database
      Public Sub Lease_Number_AfterUpdate()
      
      End Sub
      And I pasted the middle section in and tried that.

      Now, it gives me a compile error: Method or data member not found.
      It also highlights the "Public Sub..." line. it selects the ".LeaseNumb er" bit too.

      Comment

      • KelHemp
        New Member
        • Oct 2006
        • 16

        #4
        okay, I'm an idiot. I forgot to replace "TableName" with the name of my table, Products1. And I replaced the "." in each line with "!" and it works great now.

        Thanks for the help!
        Here's what I ended up with:
        Code:
        Option Compare Database
        Public Sub Lease_Number_AfterUpdate()
        
        If Me!LeaseNumber = Me!PrevLeaseNumber Then
            Me!LeaseName = DLookup("[LeaseName]", "Products1", "[LeaseNumber]=" & Me!LeaseNumber)
            Me!Location = DLookup("[Location]", "Products1", "[LeaseNumber]=" & Me!LeaseNumber)
          End If
        
        End Sub

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          LeaseNumber is Lease_Number on the form.

          Public Sub Lease_Number_Af terUpdate()

          If Me.Lease_Number = Me.PrevLeaseNum ber Then
          Me.LeaseName = Dlookup("[LeaseName]","TableNam e","[LeaseNumber]=" & Me.Lease_Number )
          Me.Location = Dlookup("[Location]","TableNam e","[LeaseNumber]=" & Me.Lease_Number )
          End If

          End Sub

          Comment

          Working...