Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Data Validation on bound form 2

Status
Not open for further replies.

tis9700

Technical User
Jun 18, 2003
100
US
Good morning,
I have a question....

I'm sure some may recognize to following code, I altered it a bit and it works great but when I click a button called "Search Students", which is a dialog, popup and modal search form to select another record, that form opens with the validation message. Is there away to cancel the underlying action to prevent the Search form from opening?

I put it in the Form_BeforeUpdate event

Dim Msg As String, Style As Integer, Title As String
Dim DL As String, ctl As Control, strControl As String

Set ctl = Screen.ActiveControl

strControl = ctl.Tag
DL = vbNewLine & vbNewLine

For Each ctl In Me.Controls
If Not IsNull(ctl.Tag) Then
If Trim(ctl & "") = "" Then
Msg = ctl.Tag & " is a Required Field!" & DL & _
"Correct this field in order to continue . . ."
Style = vbCritical + vbOKOnly
Title = "Required Data Error"

MsgBox Msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next

Thanks
 
Ah. The Aceman1 trick! I'm sure he's been asked this before but without searching, my quick answer would be to try:

a) declare a form level variable, eg: blOKToProceed

b) Form event, On Current, blOKToProceed=false

c) Add blOKToProceed=true to BeforeUpdate event above if it triggers no error (by adding an appropriate else clause of course)

d) On your search button add:

Code:
call form_BeforeUpdate
if blOKToProceed then
   docmd.openform......
else
   'nothing
endif

JB
 
Hi JBinQLD

Thanks for responding. Love the Aceman1!

I created a form level varible...

Dim Proceed as Boolean

In the Form Event, On Current ...

Proceed = False

I think I'm stuck on C)

I tried ..
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Msg As String, Style As Integer, Title As String
Dim DL As String, ctl As Control, strControl As String

Set ctl = Screen.ActiveControl

strControl = ctl.Tag
DL = vbNewLine & vbNewLine

For Each ctl In Me.Controls
If Not IsNull(ctl.Tag) Then
If Trim(ctl & "") = "" Then
Msg = ctl.Tag & " is a Required Field!" & DL & _
"Correct this field in order to continue . . ."
Style = vbCritical + vbOKOnly
Title = "Required Data Error"

MsgBox Msg, Style, Title
ctl.SetFocus
Cancel = True

Exit For
Else
Proceed = True

End If
End If
Next
End Sub

then on the button D)


Private Sub cmdSearchStudentAccounts_Click()
On Error GoTo Err_cmdSearchStudentAccounts_Click


Call Form_BeforeUpdate(1)
If Proceed Then

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmSearchStudentAccount"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close acForm, "frmStudentAccounts"
Else
'Nothing
End If


Exit_cmdSearchStudentAccounts_Click:
Exit Sub

Err_cmdSearchStudentAccounts_Click:
MsgBox Err.Description
Resume Exit_cmdSearchStudentAccounts_Click

End Sub
but it still opens. What am I doing wrong? Is it in my structure.

Thanks
 
set a break point (click on left grey margin of code) on your search button logic and step through the code (F8) hovering your mouse over the proceed variable to see why/where/when it's not being set.

It's 3.30am so i gotta grab some zzz, Im sure someone else will step in to help if you have no luck.

I'll check back to see results in a few hours

Good luck,

JB
 
How are ya tis9700 . . .

Your problem is [blue]the current record is in edit mode at the time you hit the button.[/blue] When the modal form opens it robs focus from the form, causing a save to occur which triggers the before update event. Hence you get the msgbox as well.

What you need is a way to bypass the button code [blue]if a record is in edit mode.[/blue] Basic code for the button would look like:
Code:
[blue]   Dim Msg As String, Style As Integer, Title As String
   
   If Me.Dirty Then [green]'Record in Edit Mode[/green]
      Msg = "You have to save the current record before you continue!"
      Style = vbInformation + vbOKOnly
      Title = "Operation Error! . . ."
      MsgBox Msg, Style, Title
      Screen.PreviousControl.SetFocus
   Else
      [green]'Your Button Code Here[/green]
   End If[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hi JBinQld,
Hope you got a good nights rest. I can't imagine where you are if it's 3:30 am.

I wasn't getting anything back from the varible when I Stepped Into the code.

Hi Aceman1,
I added that code to the button and it seems to be what I needed. In additon to this code and the code I have in my Form_BeforeUpdate event, I added Input Masks to the controls and a Case Select statement to my Form_Error event
and I'm set!

Thanks for everyones help!


 
tis9700 . . .

I was in a hurry when I presented the code, which was an alternate to what I intended! My intent was to disable the button as long as a record was in edit mode, enabled otherwise.

If you wish to pursue this ... let me know. The schema uses the forms [blue]Dirty[/blue] property to disable and the [blue]On Current[/blue] event to enable.

[blue]Your Thoughts . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hi Aceman1,

Sure thing, anytime I can learn someting new is a good thing. I've already learned alot from you on other issues, you've been very instrumental in my project.

And JB, thanks once again for your help. Australia! So cool!
Nice to meet you from Nashville, Tennessee.

Thanks everyone,
Tracy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top