VB Code to shut down a form

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
 
D

Douglas J. Steele

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.
 
T

tina

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
 
D

Douglas J. Steele

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
 

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

Top