Variable field names (1 Viewer)

Loy

New member
Local time
Today, 08:38
Joined
Jul 29, 2019
Messages
5
I am new to this site. This is my first post. The entire function is listed below. I need to use a variable name for 'rstGen![fDay1]' in the line

If rstGen![fDay1] = -1 Then ' if yes

There are 7 fDate fields, fDate1 thru fDate7. I need to cycle through each one. Please help!

Code:
Function completeSchedule()  '   get each record from mtGeneric and put in tblSchedule for the full week
    
    Dim x As Long
    Dim db As Database
    Dim rstSch As Recordset
    Dim rstGen As Recordset
    Dim dateDate As Date
    Set db = DBEngine(0)(0)
    Set rstSch = db.OpenRecordset("tblSchedule", dbOpenDynaset)
    Set rstGen = db.OpenRecordset("mtGeneric2", dbOpenDynaset)
    strField = "fDate1"
    dateDate = [fDateMondays]     '   date to start
'    While Not rstGen.EOF   '   records in mtGeneric2
        For x = 1 To 7  '   days in week
            rstSch.FindFirst "[fDate] = #" & dateDate & "# "
                If Not rstSch.NoMatch Then '
                        '   there are 7 fDay fields in table mtGeneric2 fDay1 thru fDay7. _
                            I need to cycle through each one
                        If rstGen![fDay1] = -1 Then '   if yes
                        
                                rstSch.Edit
                                rstSch![fTxtWorkName] = Nz(rstGen![fTxtWorkName])
                                rstSch![fTxtCrew] = Nz(rstGen![fTxtCrew])
                                rstSch![fTxtTask] = Nz(rstGen![fTxtTask])
                                rstSch![fTxtShift] = Nz(rstGen![fTxtShift])
                                rstSch![fTxtDrives] = Nz(rstGen![fDrives])
                                rstSch.Update
                        End If
                End If
        Next x
        rstGen.MoveNext
        dateDate = dateDate + x
'    Wend
    Me.Refresh
End Function
 
Last edited by a moderator:

isladogs

MVP / VIP
Local time
Today, 14:38
Joined
Jan 14, 2017
Messages
18,209
Hi Loy
Welcome to AWF
Your post was moderated which can happen when new user include a lot of code.
I've added code tags (# button on the toolbar) to improve readability
Have you considered using an update query instead of looping through a recordset?
It should be simpler to code and faster in use
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:38
Joined
Oct 29, 2018
Messages
21,454
Hi. Welcome to the forum! There are several syntax available when referring to fields, but I'll wait for your answer to Colin's question in case it's no longer necessary.
 

Loy

New member
Local time
Today, 08:38
Joined
Jul 29, 2019
Messages
5
I had not thought of a query but will try it. I will let you know
 

Loy

New member
Local time
Today, 08:38
Joined
Jul 29, 2019
Messages
5
I can't come up with any way to query into tblSchedule at the correct date!
 

June7

AWF VIP
Local time
Today, 05:38
Joined
Mar 9, 2014
Messages
5,466
Consider:

For x = 1 to 7
If rstGen("fDay" & x) = -1 Then
'do something
End If
Next
 

Loy

New member
Local time
Today, 08:38
Joined
Jul 29, 2019
Messages
5
Thank you June7. That worked GREAT.
 

Loy

New member
Local time
Today, 08:38
Joined
Jul 29, 2019
Messages
5
But I don't see how to mark it solved
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 06:38
Joined
Aug 30, 2003
Messages
36,124
No problem. I've very good at spotting other people's mistakes, not so much my own. :p
 

Users who are viewing this thread

Top Bottom