how to use Dlookup with a textbox containing a date from a combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neelsfer
    Contributor
    • Oct 2010
    • 547

    how to use Dlookup with a textbox containing a date from a combobox

    I have a combobox called Racedates and a txtbox called Racingdate on a form called CyclistF, to display a date using
    Code:
    =[racedates].[Column](1)
    i can get it to work with another textfield from the RaceEntry table but not when i use the textbox reading from the combobox.
    My code below produce no error, but is does not warn me about the duplication either.
    I assume it cant read the "racingdate " textbox.I use beforeUpdate function.
    Code:
    Dim Answer As Variant
    Answer = DLookup("raceno", "raceentry", "racedate = #" & Format(Me.RacingDate, "dd/MM/yyyy") & "#")
     If Not IsNull(Answer) Then
     MsgBox "Duplicate Race Number Found" & vbCrLf & "This Race No will now be deleted. Use a different one please", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
      Cancel = True
     Me.Undo
     End If
    Please assist
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Try a different approach:
    Code:
    If IsNull(Me![racingdate]) Or Not IsDate(Me![racingdate]) Then Exit Sub
    
    If DCount("[raceno]", "[raceentry]", "[racedate] = #" & Me.racingdate & "#") > 0 Then
      MsgBox "Duplicate Race Number Found" & vbCrLf & "This Race No will now be deleted. " & _
             "Use a different one please", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
        Cancel = True
          Me.Undo
    End If

    Comment

    • neelsfer
      Contributor
      • Oct 2010
      • 547

      #3
      Adezii, apologies i thought i got it working, but no success.
      Here is the attached file that i edited a bit, if you want to have a look pls.It just dont fo anything to warn about duplicates
      I left instructions on the screen
      Thx for your help as always
      Attached Files

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Assuming you can have Multiple Races on any given day, wouldn't you have to check for both Race Number and Race Date, as in:
        Code:
        Private Sub RaceNo_BeforeUpdate(Cancel As Integer)
        If IsNull(Me![racedate]) Or Not IsDate(Me![racedate]) Then Exit Sub
             
        If DCount("[raceno]", "[RaceEntry]", "[racedate] = #" & Me.racedate & _
                  "#  AND [raceno] = " & Me![raceno]) > 0 Then
          MsgBox "Duplicate Race Number Found" & vbCrLf & "This Race No will now be deleted. " & _
                 "Use a different one please", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
            Cancel = True
              Me.Undo
        End If
        End Sub

        Comment

        • neelsfer
          Contributor
          • Oct 2010
          • 547

          #5
          thx adezii appreciated!

          Comment

          Working...