Tech Support Guy banner
Status
Not open for further replies.

Solved: Access 03 - VBA, Forms

1K views 4 replies 2 participants last post by  ddw23 
#1 ·
I have a form I am trying to not go to a new record until certain fields have data entered by user. I have the following code, but whenever I use Access's New Button (at bottom of form), it will still go to a new record without data entry in the desired fields. These fields can not be required in the bable because it's based on a selection.
Example If user selects "House", only certain fields need to be entered, if user selects "Condo" only certain fields need to be entered. This is just an example. Below is the actual code I have so far. ----Thanks to all who can help me!!!

Private Sub Form_AfterUpdate()
Dim strUserInput

If Me.Contract_Type = "House" Then
If IsNull(Me.ITB_No) Then

strUserInput = InputBox("Please enter ITB number")

'Check to see if there is any entry made to input box, or if
'cancel button is pressed. If no entry made then exit sub.

If strUserInput = "" Or strUserInput = Empty Then
MsgBox "No Input Provided", vbInformation, "Data Required!"
DoCmd.GoToControl "ITB_No"

Exit Sub
End If

'If input entered, continue
'If input not entered, give message and exit sub

If strUserInput Is Not Null Then
Me.ITB_No = strUserInput

Else
MsgBox "You MUST enter a number", _
vbOKOnly, "Important Information!"
Exit Sub
End If
End If
End If

End Sub
 
See less See more
#3 ·
The following is what happens when I use the "BeforeUpdate"

1. I open the form
2. I select desired option "House"
3. I click on the "New Record" control at bottom of form
4. Msg pops up to enter ITB number
5. I click "cancel" button
6. Msg pops up "Data Required"
7. I click "OK" button
8. Cursor moves to the ITB No field
9. Again I click "New Record" (testing purposes)
10. And #'s 4, 5, 6 & 7 happens
11. After I press OK on the Msg, "Data Required", program goes to a new record without data being entered.

I need it to not save a record without proper input.
Thanks! That was a good suggestion...we almost had it.
 
#5 ·
This problem is solved. Below is an example of the code I used. Thanks for everyone's help.

If Me.Contract_Type = "House" Then
If ctl.Tag = "**" Or ctl.Tag = "**#" Then
If ctl = "" Or IsNull(ctl) Then
msg = "Data Required for '" & ctl.Name & "' Field." & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbCritical + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus

Cancel = True

Exit Sub
Exit For
End If
End If
End If
Next
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top