View Full Version : Addnew, based on IF in loop - problem


C.D
10-27-2007, 07:07 AM
Good day,
I'm trying to make a combined newrecord/update button for adding episodes to series, but it won't work.
Private Sub EpUpdater_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim x As Long

Set db = CurrentDb()

Set rs = db.OpenRecordset("tblAnimeEpisode", dbOpenDynaset)

For x = Me.txtStart To Me.txtEnd
If DCount("AnimeEpisodeID", "tblAnimeEpisode", "AnimID=" & [AnimeID] & " AND EpID=x") = 0 Then
rs.AddNew
rs!AnimID = Me.AnimeID
rs!EpID = x
rs.Update
End If
Next x


MsgBox "Process complete"

Set rs = Nothing
Set db = Nothing
End Sub

Thanks to pbaldy for supplying me with the basis of the code

Any help is deeply appreciated :)

pbaldy
10-27-2007, 08:50 AM
You need to concatenate the x the same way you did [AnimeID], so it's value can be interpreted for use in the formula.

RuralGuy
10-27-2007, 08:55 AM
Try putting x outside of the quotes as well:
If DCount("AnimeEpisodeID", "tblAnimeEpisode", _
"[AnimID] =" & [AnimeID] & " AND [EpID] =" x) = 0 Then

C.D
10-27-2007, 09:00 AM
You need to concatenate the x the same way you did [AnimeID], so it's value can be interpreted for use in the formula.
Yay! That worked perfectly! I thought I had done the IF and End If wrong.
This is really cool, and I never have to manually add episodes ever again :D

Thanks alot, Paul!