VB Code to shut down a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use VB to exit a form if a certain field is blank.
Currently I am using the following code, but it does not seem to work for me:
Private Sub Status_GotFocus()
If QPR_Number = "" Then

Dim stDocName As String

stDocName = "Buttons.Close Closeout QPR"
DoCmd.RunMacro stDocName

End If
End Sub
 
If Buttons.Close Closeout QPR is the name of the form (which I don't
believe, since Access doesn't allow periods in form names), and Status is on
that same form, all you should need is:

Private Sub Status_GotFocus()

If QPR_Number = "" Then
DoCmd.Close
End If

End Sub

If one or both of those assumptions is incorrect, please supply more
details.
 
from the syntax, it looks like "Buttons.Close Closeout QPR" is the
macrogroupname.macroname, which makes sense, since he's using the RunMacro
action.

i agree that the If statement should do the trick, Doug, but when i tried
running the Close command on a control's GotFocus event, i got error 2585
"This action can't be carried out while processing a form or report event."
did it run for you?

also, now that i think about it, i'm wondering if QPR_Number = "" will work,
or if James will need to use

If IsNull(QPR_Number) Then

instead.

hth
 
To be honest, I didn't test it... I see you're correct, though. Perhaps
GotFocus isn't the appropriate event, but a quick search with Google didn't
turn up what event should be used instead.

You're probably right, though, about the = "".

This would probably be better:

If Len(QPR_Number & "") = 0 Then

or even

If Len(Trim(QPR_Number & "")) = 0 Then
 
Back
Top