adapted wizard code

T

Tom

I have adapted my wizard code below to try and make sure
a record is not saved without a selection been made in
cboname this doesnt seem to work. How do I change it to
work?

Thanks

Tom

Private Sub cmdsavrecord_Click()
On Error GoTo Err_cmdsavrecord_Click

If Me.cboname Is Null Then
MsgBox ("cboname is null")
Exit Sub
Else: GoTo Save
End If

Save:
DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70
MsgBox ("saved")

Exit_cmdsavrecord_Click:
Exit Sub

Err_cmdsavrecord_Click:
MsgBox Err.Description
Resume Exit_cmdsavrecord_Click

End Sub
 
R

Rick Brandt

Tom said:
I have adapted my wizard code below to try and make sure
a record is not saved without a selection been made in
cboname this doesnt seem to work. How do I change it to
work?

Thanks

Tom

Private Sub cmdsavrecord_Click()
On Error GoTo Err_cmdsavrecord_Click

If Me.cboname Is Null Then
MsgBox ("cboname is null")
Exit Sub
Else: GoTo Save
End If

Save:
DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70
MsgBox ("saved")

Exit_cmdsavrecord_Click:
Exit Sub

Err_cmdsavrecord_Click:
MsgBox Err.Description
Resume Exit_cmdsavrecord_Click

End Sub

Are you saying that the record is still saved when they press your button
without an entry in that field or just that it is saved ultimately? There are
many actions that will cause Access to save the record and these could be
happening either before or after your button is pressed. To catch all of these
you would need your code in the BeforeUpdate event and issue a Cancel = True
when the field is blank.

The best way of course is to make that a required field in the table, then the
database engine will enforce the rule for you.
 
T

Tom

Rick,

You are right this works a lot better.

I need to actch the errors but they do not have an error
number to work with how do I do this and give the
msgboxes a title too?

Cheers

Tom
-----Original Message-----


Are you saying that the record is still saved when they press your button
without an entry in that field or just that it is saved ultimately? There are
many actions that will cause Access to save the record and these could be
happening either before or after your button is
pressed. To catch all of these
 
J

John Spencer (MVP)

First, change your test for null. In VBA you need to use the IsNull Function.

If IsNull(Me.CboName) = True Then
MsgBox "Something is blank and is required"
Else
Docmd.RunCommand acCmdSaveRecord
EndIF
 
G

Guest

Thanks John how do I give my own error messages using the
required set to yes at table level?

I am going to use this instead.

Thanks
 
J

John Spencer (MVP)

You will need to use the Form's Error Event.

Here is a sample from one of my databases.

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Dim ctlActiveControl As Control
Dim ctlActiveControlName As String

Set ctlActiveControl = Screen.ActiveControl
ctlActiveControlName = ctlActiveControl.Name

'build message strings
If DataErr = 2279 Or DataErr = 2113 Then

Select Case Me.ActiveControl.Name
Case "txtPatientDOB"
strFirstMsg = "Date of Birth"
strSecondMsg = "Invalid date format; enter as MM-DD-YYYY."

Case "txtReviewDOS"
strFirstMsg = "Date of Service"
strSecondMsg = "Invalid date format; enter as MM-DD-YYYY."

Case "txtMCOEnrollmentDate"
strFirstMsg = "Enrollment Date"
strSecondMsg = "Invalid date format; enter as MM-DD-YYYY."

Case "txtMAEligibilityCategory"
strFirstMsg = "MA Eligibility Category"
strSecondMsg = "Invalid format; enter a letter and two numbers
(example: A99)."

Case "txtPatientMANumber"
strFirstMsg = "Medicaid Number"
strSecondMsg = "Medicaid number must be exactly 11 digits long."

End Select

Else 'some other error
strFirstMsg = "Form_error"
strSecondMsg = Err.Number
strThirdMsg = Err.Description & Chr(vbKeyReturn) & "Contact your System Administrator"

End If

'Display the message
MsgBox strFirstMsg & vbcrlf & strSecondMsg & vbcrlf & strThirdMsg
Response = acDataErrContinue 'Tell Access that the error has been reported

Form_Error_Exit:
Exit Sub
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top