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!

loop in Subform

Status
Not open for further replies.

fule12

Programmer
Nov 12, 2001
140
YU
Hi All,

I’m little lost in my loop try records , so if someone can help me with this.
In subform I have 5 fields , on first four user enter data immediately but last one thay enter after one week or what ever. On subform is Add button and onclick event is this code :
Private Sub btnAdd_Click()
On Error GoTo Err_btnAdd_Click

Dim db As DAO.Database, rst As DAO.Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT Table1.SendDate FROM [Table1] WHERE Table1.MainID = " & Forms!TestAllFields.MainID)

rst.MoveFirst
Do Until rst.EOF
If IsNull(rst!SendDate) Then
MsgBox " 'Send To Acc' date is empty. Please enter date first!"
Me.AllowAdditions = False
Else

Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
End If
rst.MoveNext
Loop

rst.Close
Set rst = Nothing
Set db = Nothing


Exit_btnAdd_Click:
Exit Sub

Err_btnAdd_Click:
MsgBox Err.Number & Chr$(13) & Err.Description
Resume Exit_btnAdd_Click

End Sub

So code is working if in subform some record exist, but if in subform is no records and if I click on add button I get error : 3021 No Current record.
So I was try with : if err. Number = 3021 then Resume Next…. But now nothing happand ( no error and no new record L )
In this scenario I have two tables :
Main have two fields : MainID(PK) and one text field.
Table for subform : TestID(PK) , MainID(FK), Sum, Currency, Amount, SendDate .

Thanks

Fule
 
Your code is probably hanging on the rst.movefirst line by re-structuring the code and checking for no records you can stop the no current record error

Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT Table1.SendDate FROM [Table1] WHERE Table1.MainID = " & Forms!TestAllFields.MainID)

if rst.recordcount = 0 then
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
exit sub
end if

rst.MoveFirst
Do Until rst.EOF
If IsNull(rst!SendDate) Then
MsgBox " 'Send To Acc' date is empty. Please enter date first!"
Me.AllowAdditions = False
Else

Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
End If
rst.MoveNext
Loop

rst.Close
Set rst = Nothing
Set db = Nothing


Exit_btnAdd_Click:
Exit Sub

Err_btnAdd_Click:
MsgBox Err.Number & Chr$(13) & Err.Description
Resume Exit_btnAdd_Click

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top