Loop??

P

pvdalen

Please help! This is incredibly frustrating. :)

I have this code:

If IsNull(varGenSN) Then
Response = MsgBox("Generator does not exist. Would you like to add it
now?", vbOKCancel, "Generator Add")
If Response = vbOK Then
GenSpec.SetFocus
Else
varGenSN = ""
GenSN.SetFocus
End If
Else

The issue is I seem somehow to be in a loop? When I step through the code,
with code breaks to ensure I can step through, the first line, the If...Then
line, is executed, then the code goes right back to that line? It seemingly
ignores the code breaks?

Then, if I resume stepping through, it again goes through the first step,
then shows the MsgBox. Then, no matter what answer I give, the code again
blows through the break points and goes back to the first line, and enters a
loop that never gets past accepting the input from the MsgBox before
returning to the first line.

My "Else" line had previously been "Elseif Response = vbCancel Then". When
that was the case, the code would show me what the answer was by way of
highlighting the line of code corresponding to the answer given in the
MsgBox, then going right back to the first line.

Is there something egregioulsy wrong?

Thanks,
Paul
 
O

Ofer

Where did you put this code?
Do you have any code in the Got focus property of the fields: GenSpec,GenSN ?
You might have a loop between code that is writen in the got focus of two
fields, and this field set the focud fron one field to another.
To get help you have to spcify where is the code, and post the full code and
not just pert of it.
 
P

pvdalen

Ah. Sorry.

This code is in the Lost Focus event of the text box GenSN.

It's intended to read the text, then check the database to see if it already
exists. There's not much more too it, as I'm doing this piecemeal and tried
to see if this part would even work before going on. The portion with the
recordset has nothing to do with this and may or may not stay.

The salient point is that the program loops even while each line has a code
break.

The whole thing looks like this:

Private Sub GenSN_LostFocus()

Dim varGenSN, tGenID, Response As Variant
Dim recGenSN, recGenSpec, recGenModel, recEngine As String
Dim rs As ADODB.Recordset
Dim strSQL As String

tGenID = GenSN.Text

varGenSN = DLookup("[GenSN]", "Generator", "[GenSN] = '" & tGenID & "'")

If IsNull(varGenSN) Then
Response = MsgBox("Generator does not exist. Would you like to add it
now?", vbOKCancel, "Generator Add")
If Response = vbOK Then
GenSpec.SetFocus
Else
varGenSN = ""
GenSN.SetFocus
End If
Else

On Error GoTo errfile

strSQL = "select gensn, genspec, genmodel,enginesn from generator where
gensn = '" & tGenID & "';"
Set rs = New ADODB.Recordset
rs.Open strSQL, CurrentProject.Connection, adOpenStatic

GenSN.Text = rs(0)
GenSpec.SetFocus
GenSpec.Text = rs(1)
GenModel.SetFocus
GenModel.Text = rs(2)
EngineSN.SetFocus
EngineSN.Text = rs(3)

rs.Close

StartUp.SetFocus

End If

errfile:
Resume Next

End Sub
 
O

Ofer

Alot of set focus there that are not nede.
try this code, put it in the before update property of GenSN, let start wit
this and see where it lead us.

Dim varGenSN, tGenID, Response As Variant
Dim recGenSN, recGenSpec, recGenModel, recEngine As String
Dim rs As ADODB.Recordset
Dim strSQL As String

tGenID = GenSN.Text

varGenSN = DLookup("[GenSN]", "Generator", "[GenSN] = '" & tGenID & "'")

If IsNull(varGenSN) Then
Response = MsgBox("Generator does not exist. Would you like to add it
now?", vbOKCancel, "Generator Add")
If Response = vbOK Then
GenSpec.SetFocus
Else
'varGenSN = ""
'GenSN.SetFocus
sendkeys "{ESC}" 'will return prev value
cancel=true
End If
Else

On Error GoTo errfile

strSQL = "select gensn, genspec, genmodel,enginesn from generator where
gensn = '" & tGenID & "';"
Set rs = New ADODB.Recordset
rs.Open strSQL, CurrentProject.Connection, adOpenStatic
' I dont understand what you are trying to do here, if the records are
bounded then you will change the value in exist records
GenSN.Text = rs(0)
'GenSpec.SetFocus
GenSpec.Text = rs(1)
'GenModel.SetFocus
GenModel.Text = rs(2)
'EngineSN.SetFocus
EngineSN.Text = rs(3)

rs.Close

'StartUp.SetFocus

End If

errfile:
Resume Next

End Sub

pvdalen said:
Ah. Sorry.

This code is in the Lost Focus event of the text box GenSN.

It's intended to read the text, then check the database to see if it already
exists. There's not much more too it, as I'm doing this piecemeal and tried
to see if this part would even work before going on. The portion with the
recordset has nothing to do with this and may or may not stay.

The salient point is that the program loops even while each line has a code
break.

The whole thing looks like this:

Private Sub GenSN_LostFocus()

Dim varGenSN, tGenID, Response As Variant
Dim recGenSN, recGenSpec, recGenModel, recEngine As String
Dim rs As ADODB.Recordset
Dim strSQL As String

tGenID = GenSN.Text

varGenSN = DLookup("[GenSN]", "Generator", "[GenSN] = '" & tGenID & "'")

If IsNull(varGenSN) Then
Response = MsgBox("Generator does not exist. Would you like to add it
now?", vbOKCancel, "Generator Add")
If Response = vbOK Then
GenSpec.SetFocus
Else
varGenSN = ""
GenSN.SetFocus
End If
Else

On Error GoTo errfile

strSQL = "select gensn, genspec, genmodel,enginesn from generator where
gensn = '" & tGenID & "';"
Set rs = New ADODB.Recordset
rs.Open strSQL, CurrentProject.Connection, adOpenStatic

GenSN.Text = rs(0)
GenSpec.SetFocus
GenSpec.Text = rs(1)
GenModel.SetFocus
GenModel.Text = rs(2)
EngineSN.SetFocus
EngineSN.Text = rs(3)

rs.Close

StartUp.SetFocus

End If

errfile:
Resume Next

End Sub

Ofer said:
Where did you put this code?
Do you have any code in the Got focus property of the fields: GenSpec,GenSN ?
You might have a loop between code that is writen in the got focus of two
fields, and this field set the focud fron one field to another.
To get help you have to spcify where is the code, and post the full code and
not just pert of it.
 
Top