Prevent data duplication with condition - vba

I want to setup a data duplication check on a text box which is used to input serial numbers.
If the entered serial number is already found in the database, it should call a MsgBox to alert the user before clearing the value in the text box.
However, if the entered serial number contains "RW", the check should be disabled.
Private Sub Serial_Number_AfterUpdate()
Dim NewSerialNumber As String
Dim stLinkCriteria As String
NewSerialNumber = Me.Serial_Number.Value
stLinkCriteria = "[Serial_Number] = " & "'" & NewSerialNumber & "'"
If Me.Serial_Number = DLookup("[Serial_Number]", "Esagon_End", stLinkCriteria) Then
MsgBox "This serial number, " & NewSerialNumber & ", has already been entered into the database." _
& vbCr & vbCr & "Please check the serial number again.", vbI, "Duplicate information"
Me.Undo
End If
End Sub
If this cannot be done with VBA I'm open to other methods like queries. Thank you.

Looking at what you have asked, I think this is what you are looking for. If it isn't then leave a comment and I'll try to update my answer.
Private Sub Serial_Number_AfterUpdate()
'If it doesn't contain "RW"
If InStr(Me.Serial_Number, "RW") = 0 Then
'If serial number not in the database
If DCount("*", "Esagon_End", "Serial_Number = '" & Me.Serial_Number & "'") > 0 Then
'Alert user and blank the text box
Call MsgBox("The serial number " & Me.Serial_Number & " is already in the database." _
& vbCrLf & vbCrLf & "Please check the serial number you are entering.", _
vbInformation, "Duplicate Serial")
Me.Serial_Number = ""
End If
End If
End Sub

Related

Run-Time Error '13' Type Mismatch - ACCESS DATABASE

I am trying to compare two text fields txtTrailerNumber and txtSealNumber to the database table Tab_TrailerDetails. [TrailerNumber] and [SealNumber] as listed in the table.
I am trying to get the database to look at the trailer number entered into the form, and if it finds a duplicate value it then looks at the seal number entered into the form. If both values have a duplicate found in the table it should throw up the Msg_Box error code.
Private Sub txtSealNumber_AfterUpdate()
Dim NewTrailer, NewSeal As String
Dim stLinkCriteria As String
'Assign the entered Trailer Number and Seal Number to a variable
NewTrailer = Me.txtTrailerNumber.Value
NewSeal = Me.txtSealNumber.Value
stLinkCriteria = ("[TrailerNumber]='" & NewTrailer & "'" And "[SealNumber]='" & NewSeal & "'")
If Me.txtTrailerNumber = DLookup("[TrailerNumber]", "Tab_TrailerDetails", stLinkCriteria) Then
MsgBox "This trailer, " & NewTrailer & ", has already been entered in database," _
& vbCr & vbCr & "along with seal " & NewSeal & "" _
& vbCr & vbCr & "Please make sure Trailer and Seal are not already entered.", vbInformation, "Duplicate information"
'undo the process and clear all fields
Me.Undo
End If
End Sub
The cause of the error is that you have a logical keyword, notably AND inside a string expression. Change your code to
stLinkCriteria = ("[TrailerNumber]='" & NewTrailer & "' And [SealNumber]='" & NewSeal & "'")

Duplicate message problem in MS Access VBA code

Still new to coding in MS Access. Need some assistance.
What I want to be able to do is capture a person in my table multiple based off the request date however if the request is the same date I want my message for duplicate record to show. Right now my code is looking at first name and last name only and if I enter the same person in on my form with a different date it thinks its a duplicate. Need to fix this. below is my code. I want to add something in this to look at the date as well as the names. The field for the date is Date_of_DMIPasswordReset
Private Sub cmdSave_Click()
If DCount("*", "Tbl_DMIPasswordResets", "FirstName = '" & Me.txtFirstName & "' AND LastName = '" &
Me.txtLastName & "'") > 0 Then
If MsgBox(Me.txtFirstName & " " & Me.txtLastName & " Is already in the DMI Password Reset table
to be approved" & vbNewLine & _
"Do You want to Delete this record", vbQuestion + vbYesNo, "Duplicate Entry") = vbYes Then
Me.Undo
End If
Else
Form_BeforeUpdate (0)
If blnSaveRecord Then
Me.Dirty = False
MsgBox "DMI Password Reset for this Employee Saved", vbInformation
Me.cboEmpLookup.Requery
End If
End If
End Sub

When DoCmd.OpenForm opens a blank form because no record was found, display a message box that says record is not found

I have a MainMenu where the user can enter a SchoolID in a search bar (txtSearchBar) and when they click on the SearchBySchoolID button, it opens the form, SchoolForm, based on the SchoolID. Sometimes the user clicks the SearchBySchoolID button without entering anything in the txtSearchBar so when the code below is executed, SchoolForm still opens but it is all blank.
What can I add to my code below so that a message box saying "No SchoolID found" pops up instead of bringing the user to a blank SchoolForm when they type nothing into my search bar?
Private Sub SearchBySchoolID_Click()
Dim txtSearchBar As String
On Error GoTo ErrorIDSearch
DoCmd.OpenForm "SchoolForm", , , "SchoolID = " & ("""" &
Me.txtSearchBar.Value & """"), acFormReadOnly
ExitErrorIDSearch:
Exit Sub
ErrorSIDSearch:
If Err.Number = 3075 Then
MsgBox "Please enter a valid SchoolID."
Else
MsgBox "The following error has occured:" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & vbCrLf & _
"Error Description: " & Err.Description & vbCrLf & vbCrLf & , _
vbCritical, "An Error has Occured!"
Resume ExitErrorIDSearch
End If
End Sub
You have two choices
Either put your event handling in the OnOpen event of the form named SchoolForm (and maybe use OpenArgs or an invisible textbox to set some kind of Status on that form so it knows where it was opened from, and why)
or simpler:
Check for existence of the SchoolID in the table before attempting to open SchoolForm
Could use a DLookup or a DCount
e.g.
If DCount("SchoolID", "YourTableOrQueryForSchools", "SchoolID=" & """" & Me.txtSearchBar.Value & """") = 0 Then
MsgBox "Please enter a valid SchoolID.", 64, "Try Again"
Me.txtSearchBar = ""
Me.txtSearchBar.Setfocus
Else
DoCmd.OpenForm "SchoolForm", , , "SchoolID = " & """" & Me.txtSearchBar.Value & """", acFormReadOnly
End If

On Click command and not adding text when text box is blank

On Click I want my button to not add text when me.txtAddNote is blank and display a message prompting the user to enter text or cancel. And when me.txtAddNote has text I would like the On Click to enter the text. Currently, my code adds text on both conditions and even before the msgbox pops up. Any help is appreciated, thank you.
Private Sub cmdAddNote_Click()
Dim LName As String
On Error Resume Next
LName = DLookup("[LNAME]", "[qryEmpDepDes]", "[EMP_NO]='" & Me.txtUserID & "'")
[NOTES] = Date & ": " & Me.txtAddNote & " (" & Me.txtUserID & " " & LName & ")" & vbNewLine & vbNewLine & [NOTES]
Me.txtAddNote = ""
Me.cmdAddNote.Enabled = True
Me.cmdClose.Enabled = True
Me.cmdClose.SetFocus
'5-16-2016 testing blank text box'
If Me.txtAddNote = "" Then
If MsgBox("No text is entered. Hit OK to enter text. Hit CANCEL to close out.", vbOKCancel) = vbOK Then
End If
Else
DoCmd.Close
End If
End Sub
Try checking the content of txtAddNote prior to anything else?
Private Sub cmdAddNote_Click()
Dim LName As String
If Me.txtAddNote.Text = "" Then
response = MsgBox("No text is entered. Hit OK to enter text. Hit CANCEL to close out.", vbOKCancel)
If response = vbOK Then
' do whatever you needed
Else
Exit Sub ' Exit the sub if Cancel was clicked
End If
End If
On Error Resume Next
LName = DLookup("[LNAME]", "[qryEmpDepDes]", "[EMP_NO]='" & Me.txtUserID & "'")
[NOTES] = Date & ": " & Me.txtAddNote & " (" & Me.txtUserID & " " & LName & ")" & vbNewLine & vbNewLine & [NOTES]
Me.txtAddNote = ""
Me.cmdAddNote.Enabled = True
Me.cmdClose.Enabled = True
Me.cmdClose.SetFocus
DoCmd.Close
End Sub

How to create a comment box with time and date stamp in Access using VBA

Completely new to VBA and need help with detailed instructions (dummy version for me).
I have a table with various columns and the following columns, specifically:
ReviewerComments
NewComment
I created a form with both of these fields and need to create an Append Comment button that moves the text from NewComment field and appends it to ReviewerComment field and time/date stamps the comments as they are added. I named this button cmdAppendComment.
I had seen someone else post something and I tried, but as I am completely new to this I know I messed it up. Any help is greatly appreciated.
This is what the VBA code looks like right now:
Private Sub cmdAppendComment_Click()
If (IsNull(NewComment.Value)) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(ReviewerComments.Value)) Then
ReviewerComments.Value = NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
ReviewerComments.Value = ReviewerComments.Value & _
vbNewLine & vbNewLine & _
NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
NewComment.Value = ""
End Sub
I have some suggestions for your code:
1. Do not check if a textbox is null but how many characters your textbox has. You should always do it that way because otherwise you tend to get errors.
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
Here you check the length of the string in your textbox. You need to append "" because otherwise you tend to get null-errors or something similar.
2. You forgot to reference the objects in your form correctly. You have your form and in that form you put your textboxes and also your VBA-code. Your can reference all your objects with "Me.[FormObjects]".
The compiler complains that "NewComment.Value" or "ReviewerComment.Value" is not initialized or in other words not dimensioned. With correct reference this should stop.
Private Sub cmdAppendComment_Click()
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(Me.ReviewerComments.Value)) Then
Me.ReviewerComments.Value = Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
Me.ReviewerComments.Value = Me.ReviewerComments.Value & _
vbNewLine & vbNewLine & _
Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
Me.NewComment.Value = ""
End Sub