Question Locked

VB Question

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello,

I am relatively new visual basic and am struggling with some code and was wondering if anyone else out there can help me.

I am using VB in access 2003 and have a form with several fields on it. There is six fields that are required on the form. I have set a add record button and am using the on click event to make sure these six fields are not null and not zero.

I have it set so that if the routine detects a field that is null or zero in length it flips up a message box. My intent was to stop the rest of the code from processing and the focus to be reset to the field that needed the data entered but when I go to execute it runs fine without error but does not stop the rest of the routine from executing. It's like I can't quite grasp one little line that tells the form to stop everything. So that is what I am needing help with. I will post a portion of the code here.

'Evaluate Section Code Combo Box
If Me.cboSectionCode.Value = "" Or (IsNull(Me.cboSectionCode.Value)) Then
flgSectionCode = False
EnterMsgBox = MsgBox("Section Code is a required field, please select data and continue.", vbOKCancel, "Required Field Error")
If VbMsgBoxResult = 1 Then
Me.cboSectionCode.SetFocus
End If
End If

5 replies

by larryd4

When you create a save record button, which I assume you did with the Wizard. It uses the OnClick event and it creates the code to save.

Example:
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click

End Sub

So from what I see in the code that you posted. Is that you don't even have the process to save the record.

I would assume the OnClick event should look something like this..

Private Sub Command0_Click()
On Error GoTo Err_Command0_Click

If Me.cboSectionCode.Value = "" Or (IsNull(Me.cboSectionCode.Value)) Then
flgSectionCode = False
EnterMsgBox = MsgBox("Section Code is a required field, please select data and continue.", vbOKCancel, "Required Field Error")
End If
If VbMsgBoxResult = 1 Then
Me.cboSectionCode.SetFocus
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End If

Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click

End Sub

by omnknt232006

But I left that part off of the post. That part of the code is currently commented out because I don't want to write to the db yet, i mainly want to make sure my coding is working so it is more or less a normal command button right now. My code looks similar except for the form refresh lines you put in. (Docmd....) I will test out your suggestion in the morning to see if it works.

by 45geek

First VB gets confused if not nested properly.

If some expression
If some expression
End If
End If

also to end any if statement use the "else" for the condition false with code to exit or refocus. Also if using the expression to validate to a null or no entry there has to be a space bewteen the quotes (e.g. value = "(space)" not "")
'Example
If me.cboSectionCone.Value = " " Or (IsNull(Me.cboSectionCode.Value)) Then
flgSectionCode = False
EnterMsgBox = MsgBox("Section Code is a required field, please select data and continue.", vbOKCancel, "Required Field Error")
Else
Me.cboSectionCode.SetFocus
If VbMsgBoxResult = 1 Then
Me.CboSectionCode.SetFocus
End If
End If

Hope that helps.

by omnknt232006

It was nested properly but the question text box on this page did not put in the tabs. I tried the else statement but it didn't halt the routine, it just processed the rest of it and then once all the code had executed it set the focus into the box. I was hoping for the error to halt all processing if the user encountered the message box. I hope I am not confusing everyone. Thanks for the help.

This discussion is closed to new replies.