MySql Access VB - mysql

My application is
Microsoft Access front end
MySql back end
Code is in vb
I am trying to modify a form that has a few drop down fields [![enter image description here][1]][1]. I need it to require the jobcustomerID before updating. Right now if i add an address first it will tell me to add the customer but gives me an error for "invalid use of null"
Current Code
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.JobCustomerID) Or Me.JobCustomerID = "" Then
Call MsgBox("You must select a Customer from the list provided", vbExclamation, "REQUIRED ENTRY")
Me.JobCustomerID.SetFocus
Cancel = True
Exit Sub
End If
End Sub
I do have an image of the fields but lack the reputation needed

Try moving the cancellation to the front.
If IsNull(Me.JobCustomerID) Or Me.JobCustomerID = "" Then
Cancel = True
MsgBox "You must select a Customer from the list provided", vbExclamation, "REQUIRED ENTRY"
Me.JobCustomerID.SetFocus
Exit Sub
End If

Related

if the Text field is blank then i want user to enter date with msgbox message

I have a text224 in a MS access form.
I want to check if the user is entering date or not. If date is not entered then i want to force user to enter date.
I tried this code and it is not working.
Private Sub Command240_Click()
If Me.Text224 = "" Then
MsgBox "Date is blank"
Else
MsgBox "date function not working "
End If
End Sub
There are several ways to handle the user input, for example:
Private Sub Command240_Click()
If IsNull(Me.Text224) Then
MsgBox "Before I can Command240, you must enter a Text224.", vbExclamation
ElseIf Not IsDate(Me.Text224) Then
MsgBox "The Text224 you entered is invalid.", vbExclamation
Else
MsgBox "You have entered a valid Text224, but for some reason I can't Command240."
End If
End Sub
Also, there is a way to name your controls.

MS Access Error '2110' Can't move the focus to the control

I'm a beginner using MS Access 2016.
I have a save button on a form that does some last minute input validation that is meant to be very direct and helpful to the end-user. I'm using a series of textboxes to step through the validation when they click the save button, this way they can complete then entire form and if there is an error it takes them directly to that field with the error.
My code appears to work,
it detects an error,
sets the focus to the text179,
sees there is an error in the JobNo field,
gives a message saying "Please enter a 5 digit job number"and
sets the focus to JobNo.
Then and only then, do I get a second prompt saying there is an error.
But why is it saying it can't set the focus to text179 when it clearly already has done so and should no longer be trying at that point?
Here is my code:
Private Sub SaveRecord_GotFocus()
If Me.JobNo & "" Like "#####" And Me.ItemNo & "" <> "" Then
Exit Sub
Else
Me.Text179.SetFocus
End If
End Sub
Private Sub Text179_GotFocus()
If Me.JobNo & "" Like "#####" Then
Me.Text181.SetFocus
Exit Sub
Else
MsgBox "Please enter a 5 digit Job Number", vbOKOnly
Cancel = True
Me.JobNo.SetFocus
Exit Sub
End If
End Sub
Error:
Run-time error '2110':
Microsoft Access can't move the focus to the control Text179.
Let me know if any additional information is needed.
I was able to solve this with a much cleaner method, using just VB code without trying to use setfocus. It just took me a while to figure out how to nest my if statements correctly since I'm new with Access/VB. I'll post the code below.
Private Sub SaveRecord_GotFocus()
If Me.JobNo & "" Like "#####" Then
GoTo Eval_ItemNo
Else
MsgBox "Please enter a 5 digit Job Number", vbOKOnly
Me.JobNo.SetFocus
Exit Sub
End If
Eval_ItemNo:
If Me.ItemNo & "" <> "" Then
GoTo Eval_QtyFinished
Else
MsgBox "Please enter an Item Number", vbOKOnly
Me.ItemNo.SetFocus
Exit Sub
End If
Eval_QtyFinished:
If Me.QtyFinished & "" <> "" Then
GoTo Eval_WorkCenter
Else
MsgBox "Please enter a Qty Finished", vbOKOnly
Me.QtyFinished.SetFocus
Exit Sub
End If
Eval_WorkCenter:
If Me.WorkCenter & "" <> "" Then
Exit Sub
Else
MsgBox "Please enter a WorkCenter", vbOKOnly
Me.WorkCenter.SetFocus
End If
End Sub

Access Input Form with Custom Error Messages

The Solution
The solution was to not try and capture the errors but do the error handling myself as part of the Add New Record command button:
Private Sub buttonNewRecord_Click()
Dim ErrorInt As Integer
Dim TeleCheck As Variant
Name.SetFocus
If Name.Text = "" Then
MsgBox "Name is missing!"
ErrorInt = ErrorInt + 1
End If
TeleCheck = DLookup("[Telephone]", "tblColdCallLog", "[Telephone] = '" & Me.Telephone & "'")
If Not IsNull(TeleCheck) Then
MsgBox "Telephone number already exists in the table!"
ErrorInt = ErrorInt + 1
End If
If ErrorInt < 1 Then
DoCmd.GoToRecord , , acNewRec
MsgBox "Record Added!"
End If
End Sub
Original Post:
What I Have:
I have created a simple Access 2013 Form used to input data into a table. On the Form, the user enters data into the fields and clicks on a button made using the Command Button Wizard to Add New Record.
The form has one required field, [Name], and one field set to 'Index: Yes (No Duplicates)', [Telephone Number]. In the Form, this correctly produces error messages if the [Name] field is empty or there is a duplicate number detected in the [Telephone] field that is also in the table.
What I Am Trying To Do:
The error messages that appear are not user friendly. I would like to replace them with custom error messages and if there are no errors, maybe a message that says all went well.
What I Have Tried:
On the Form properties, Events tab, in 'On Error', [Event Procedure]:
Private Sub Error_Sub(DataErr As Integer, Response As Integer)
If DataErr = 3022 Then
MsgBox "Duplicate telephone number found in table!"
Response = acDataErrContinue
End If
If DataErr = 3314 Then
MsgBox "Name is missing!"
Response = acDataErrContinue
End If
End Sub
This works but only when you close the Form... When you click the 'Add New Record' Command Button, it simply shows the default error messages when appropriate.
Maybe I should use the Event 'Before Update'? I can't seem to use the same VBA script. I'm not allowed to define DataErr or Response. So, I'll use an Expression instead:
=IIf(Error(3022),MsgBox("Duplicate telephone number found in table"))
=IIf(Error(3314),MsgBox("Name is missing"))
This works... but when there is no error. Even if there is a name in the [Name] field, the error shows but at least it replaces the default error message.
Let's put it in the button itself? I'll have to use the Macro Builder to edit it. It's a bit hard to copy and paste this one so I'll simplify:
OnError GoTo Error_Handling
GoToRecord New
If [MacroError]<>0 Then
MsgBox = "[MacroError].[Description]"
End If
Error_Handling:
If Error(3022) Then
MsgBox = "Duplicate telephone number found in table!"
End If
If Error(3314) Then
MsgBox = "Name is missing!"
End If
This does the same as the 'Before Update' event; replaces the default error message but regardless of whether or not the error message should be triggered in the first place.
What am I doing wrong? I get the feeling it's something really simple. I've tried a variety of other combinations and endless Googling but I feel stumped.
Private Sub buttonNewRecord_Click()
Dim ErrorInt As Integer
Dim TeleCheck As Variant
Name.SetFocus
If Name.Text = "" Then
MsgBox "Name is missing!"
ErrorInt = ErrorInt + 1
End If
TeleCheck = DLookup("[Telephone]", "tblColdCallLog", "[Telephone] = '" & Me.Telephone & "'")
If Not IsNull(TeleCheck) Then
MsgBox "Telephone number already exists in the table!"
ErrorInt = ErrorInt + 1
End If
If ErrorInt < 1 Then
DoCmd.GoToRecord , , acNewRec
MsgBox "Record Added!"
End If
End Sub

Check Access Form Field for Null, if Not Null then Call the Add Feature

I have this simple Access Form that when you fill out the form and forget to fill out the Business Unit field, a msgBox will pop up telling you so and setFocus to that very Combo box. If it is Not null, I want to call the next feature. The first part of this code works, but the when it is notNull it wil not carry on.
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then
MsgBox "Please Select a Business Unit!", vbOKOnly
Exit Sub
End If
Me.Combo_BU_Selector.SetFocus
Exit Sub
If Not IsNull(Me.BU_Selected_Add) Then
Call Add_RPS_LINE
End If
End Sub
Does anybody see where I am totally out in left field?
You've got an extra Exit Sub (the one after the first MsgBox) that stops your code from doing what you want. Also, your first End If is in the wrong location.
Try something like this instead:
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then ' No business unit
MsgBox "Please Select a Business Unit!", vbOKOnly ' Tell user
Me.Combo_BU_Selector.SetFocus ' Focus the control
Exit Sub ' Exit the method
End If ' End the IsNull test
Call Add_RPS_LINE ' You only get here if the above doesn't execute
End Sub
It helps if you learn to properly indent your code to match If and End If visually, so you can see where they line up (match) and where they don't. :-)
If you correct the indentation of your code to:
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then
MsgBox "Please Select a Business Unit!", vbOKOnly
Exit Sub
End If
Me.Combo_BU_Selector.SetFocus
Exit Sub
If Not IsNull(Me.BU_Selected_Add) Then
Call Add_RPS_LINE
End If
End Sub
You can clearly see that the Exit Sub in the middle will terminate the function before it gets to Call Add_RPS_LINE.
If you look at the two If statements you have, you can see that they are almost the same and so an Else is in order, resulting in this simpler and more readable code.
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then
MsgBox "Please Select a Business Unit!", vbOKOnly
Me.Combo_BU_Selector.SetFocus
Else
Call Add_RPS_LINE
End If
End Sub

MS Access form to edit specific record from a form by providing input through text box

Can someone please help me on this find specific record Edit through MS access Forms
I have Frmfind form where I have one filed "ticket#" is a input text box
another filed was button "find"
When I enter ticket# which is primary key for my table. I need get the the specific ticket# record should be opened in FormEdit mode using VBA code...
So I have another form "frmEdit" of specific record which has to be called from frmfind -> specific input..
note: Ticket# is column in my table whcih it is primary to have the ticket#.
Code:
Option Compare Database
Private Sub find_Click()
If IsNull(Me.Text79) Or Me.Text79 = "" Then
MsgBox "You must enter a Ticket #", vbOKOnly, "Required Data"
Me.Text79.SetFocus
Exit Sub
End If
If [Ticket#] = Me.Text79.Value Then
MsgBox "Record found"
DoCmd.Close
DoCmd.OpenForm "frmEdit"
Else
MsgBox "not matching record"
Me.Text79.SetFocus
End If
End Sub
Private Sub Form_Open(cancel As Integer)
'On open set focus to text box
Me.Text79.SetFocus
End Sub
You can use this code:
Private Sub Command112_Click()
Me.txtSeach.SetFocus
On Error Resume Next
DoCmd.OpenForm "frmEdit", , , "TicketNumber = '" & Nz(Me.text79, "") & "'"
End Sub
Note in above if TicketNumber is in fact a number column and not text, then remove the single quotes and use this:
DoCmd.OpenForm "aa", , , "TicketNumber = " & Nz(Me.text79, "")
Then for your message, just place that code in the forms on-open event that has a cancel:
eg:
Private Sub Form_Open(Cancel As Integer)
If IsNull(Me!TicketNumberID) Then
MsgBox "Please enter a valid Ticket #", vbOKOnly, "Required Data"
Cancel = True
End If
End Sub
The above assumes your search column is ticket number.
You can also use dlookup(), or even dcount(). I think above is less code, but:
Dim strWhere As String
strWhere = "TicketNumber = " & Me.text79
If DCount("*", "tblBookings", strWhere) > 0 Then
code for Record found goes here
Else
code reocrd not found code here
End If
So either way should suffice here.