FORM CLOSING PROBLEM

B

Bre-x

I have a form (SW) with one single object, a text box (EMP).
I get an error: This action cannt be carried out while processing a form
event

What i want is that when the user enter his ID, then press the "Enter" key,
close the form and open another.

Thanks,

Bre-x


This is my code:

Private Sub EMP_Enter()
On Error GoTo Err_EMP_Enter
Dim sqlstring As String

If IsNull(Me.EMP) = True Then
Exit Sub
End If

sqlstring = "SELECT Count(*) AS CC FROM EMP WHERE EMP_NUM=" & Me.EMP
CurrentDb.QueryDefs("MS").SQL = sqlstring

If DLookup("[CC]", "MS") = 0 Then
Responce = MsgBox("WRONG EMP ID ", vbCritical, " MYCOMPANY")
Me.EMP = Null
Me.EMP.SetFocus
Exit Sub
Else
'SETUP MAIN FORM
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE FMAIN SET EMP = " & Me.EMP
DoCmd.SetWarnings True
DoCmd.OpenForm "MAIN"
DoCmd.Close acForm, "SW"
End If

Exit_EMP_Enter:
Exit Sub
Err_EMP_Enter:
MsgBox Err.Description & " " & Err.Number
Resume Exit_EMP_Enter
End Sub
 
G

Guest

You don't say where you are getting the error.
Looking at your code, it appears you are overcomplicating the problem. If,
as I read it, you are trying to be sure the employee number entered is in the
table before you open the form Main, then you might want to try this version:

Private Sub EMP_Enter()

On Error GoTo Err_EMP_Enter

If IsNull(Me.EMP) = True Then
Exit Sub
End If

If IsNull(DLookup("[EMP_NUM], "EMP", "[EMP_NUM] = " & Me.EMP)) Then
MsgBox "WRONG EMP ID ", vbCritical, " MYCOMPANY")
Me.EMP = Null
Me.EMP.SetFocus
Else
'SETUP MAIN FORM
CurrentDb.Execute("UPDATE FMAIN SET EMP = " & Me.EMP), dbFailOnError
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "MAIN"
End If

Exit_EMP_Enter:
Exit Sub
Err_EMP_Enter:
MsgBox Err.Description & " " & Err.Number
Resume Exit_EMP_Enter
End Sub
 
B

Bre-x

The error occurs when i try to close the "SW" form.

I tried even to enter docmd.close acform, "SW" on the open form event on the
main form and again an error code.
is like the SW form is carrying a event as there error msg says.

thnks

Bre-x




Klatuu said:
You don't say where you are getting the error.
Looking at your code, it appears you are overcomplicating the problem.
If,
as I read it, you are trying to be sure the employee number entered is in
the
table before you open the form Main, then you might want to try this
version:

Private Sub EMP_Enter()

On Error GoTo Err_EMP_Enter

If IsNull(Me.EMP) = True Then
Exit Sub
End If

If IsNull(DLookup("[EMP_NUM], "EMP", "[EMP_NUM] = " & Me.EMP)) Then
MsgBox "WRONG EMP ID ", vbCritical, " MYCOMPANY")
Me.EMP = Null
Me.EMP.SetFocus
Else
'SETUP MAIN FORM
CurrentDb.Execute("UPDATE FMAIN SET EMP = " & Me.EMP),
dbFailOnError
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "MAIN"
End If

Exit_EMP_Enter:
Exit Sub
Err_EMP_Enter:
MsgBox Err.Description & " " & Err.Number
Resume Exit_EMP_Enter
End Sub


Bre-x said:
I have a form (SW) with one single object, a text box (EMP).
I get an error: This action cannt be carried out while processing a form
event

What i want is that when the user enter his ID, then press the "Enter"
key,
close the form and open another.

Thanks,

Bre-x


This is my code:

Private Sub EMP_Enter()
On Error GoTo Err_EMP_Enter
Dim sqlstring As String

If IsNull(Me.EMP) = True Then
Exit Sub
End If

sqlstring = "SELECT Count(*) AS CC FROM EMP WHERE EMP_NUM=" & Me.EMP
CurrentDb.QueryDefs("MS").SQL = sqlstring

If DLookup("[CC]", "MS") = 0 Then
Responce = MsgBox("WRONG EMP ID ", vbCritical, " MYCOMPANY")
Me.EMP = Null
Me.EMP.SetFocus
Exit Sub
Else
'SETUP MAIN FORM
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE FMAIN SET EMP = " & Me.EMP
DoCmd.SetWarnings True
DoCmd.OpenForm "MAIN"
DoCmd.Close acForm, "SW"
End If

Exit_EMP_Enter:
Exit Sub
Err_EMP_Enter:
MsgBox Err.Description & " " & Err.Number
Resume Exit_EMP_Enter
End Sub
 
G

Guest

I don't know if this is the problem, but it may be a timing issue. It is
possible the query has not completed when you try to close the form:

Have you tried my version to see if it helps?

Bre-x said:
The error occurs when i try to close the "SW" form.

I tried even to enter docmd.close acform, "SW" on the open form event on the
main form and again an error code.
is like the SW form is carrying a event as there error msg says.

thnks

Bre-x




Klatuu said:
You don't say where you are getting the error.
Looking at your code, it appears you are overcomplicating the problem.
If,
as I read it, you are trying to be sure the employee number entered is in
the
table before you open the form Main, then you might want to try this
version:

Private Sub EMP_Enter()

On Error GoTo Err_EMP_Enter

If IsNull(Me.EMP) = True Then
Exit Sub
End If

If IsNull(DLookup("[EMP_NUM], "EMP", "[EMP_NUM] = " & Me.EMP)) Then
MsgBox "WRONG EMP ID ", vbCritical, " MYCOMPANY")
Me.EMP = Null
Me.EMP.SetFocus
Else
'SETUP MAIN FORM
CurrentDb.Execute("UPDATE FMAIN SET EMP = " & Me.EMP),
dbFailOnError
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "MAIN"
End If

Exit_EMP_Enter:
Exit Sub
Err_EMP_Enter:
MsgBox Err.Description & " " & Err.Number
Resume Exit_EMP_Enter
End Sub


Bre-x said:
I have a form (SW) with one single object, a text box (EMP).
I get an error: This action cannt be carried out while processing a form
event

What i want is that when the user enter his ID, then press the "Enter"
key,
close the form and open another.

Thanks,

Bre-x


This is my code:

Private Sub EMP_Enter()
On Error GoTo Err_EMP_Enter
Dim sqlstring As String

If IsNull(Me.EMP) = True Then
Exit Sub
End If

sqlstring = "SELECT Count(*) AS CC FROM EMP WHERE EMP_NUM=" & Me.EMP
CurrentDb.QueryDefs("MS").SQL = sqlstring

If DLookup("[CC]", "MS") = 0 Then
Responce = MsgBox("WRONG EMP ID ", vbCritical, " MYCOMPANY")
Me.EMP = Null
Me.EMP.SetFocus
Exit Sub
Else
'SETUP MAIN FORM
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE FMAIN SET EMP = " & Me.EMP
DoCmd.SetWarnings True
DoCmd.OpenForm "MAIN"
DoCmd.Close acForm, "SW"
End If

Exit_EMP_Enter:
Exit Sub
Err_EMP_Enter:
MsgBox Err.Description & " " & Err.Number
Resume Exit_EMP_Enter
End Sub
 

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