Results 1 to 7 of 7
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095

    variable used in DAO Recordset edit expression

    Updating records in a DAO Recordset:



    Code:
    !["F" & strSunCol] = Format(DteValue, "dddd, mmmm, dd, yyyy")
    The field names are of the form "F2", "F3",,,,,,etc. The numerical suffix isn't known until the code runs at entry. What's the syntax of such an assignment?

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Have you tried Me.Controls("F" & strSunCol)?
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    I'm updating a DAO Recortset, not a control. I had just found the answer when your post came in.

    Code:
            .Edit
            strFieldName = "F" & strSunCol
            .Fields(strFieldName) = Format(DteValue, "dddd, mmmm, dd, yyyy")
            
            strFieldName = "F" & strSunCol + 1
            .Fields(strFieldName) = Format(DteValue + 1, "dddd, mmmm, dd, yyyy")
            
            strFieldName = "F" & strSunCol + 2
            .Fields(strFieldName) = Format(DteValue + 2, "dddd, mmmm, dd, yyyy")
            
            strFieldName = "F" & strSunCol + 3
            .Fields(strFieldName) = Format(DteValue + 3, "dddd, mmmm, dd, yyyy")
            
            strFieldName = "F" & strSunCol + 4
            .Fields(strFieldName) = Format(DteValue + 4, "dddd, mmmm, dd, yyyy")
            
            strFieldName = "F" & strSunCol + 5
            .Fields(strFieldName) = Format(DteValue + 5, "dddd, mmmm, dd, yyyy")
                    
            strFieldName = "F" & strSunCol + 6
            .Fields(strFieldName) = Format(DteValue + 6, "dddd, mmmm, dd, yyyy")
            .Update
    The "Fn" fields are populated with successive increasing dates. The primary issue is that the starting field name isn't known until runtime.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Glad to hear you got it working! But wasn't your initial unedited post about a control on a form? Why edit the initial post, makes mine irrelevant.....
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    Sorry to confuse things. Perhaps if my OP would have read: Updating record fields in a DAO Recordset: might have steered one away from thinking controls? At the time I think I thought the use of "splat" would give a hint. Anyway, again, sorry for the mislead.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    So you could just as easy used .Fields("F" & strSunCol) as per Vlads suggestion?

    Plus I would have used a loop for anything like that.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    Plus I would have used a loop for anything like that.
    Yes indeed. Once I got the looping values clear I re-coded that section of the sub.
    This is better and much easier to follow:
    Code:
    Public Sub Set7Days(DteValue As Long)
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  We need to update the date fields in the date columns in table tblMenuSheet
    '  tblMenuSheet columns AND rows can vary as found in tblSettings plus the
    '  7 day columns found in the base Excel file.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim strSQL As String
    Dim strFieldName As String
    Dim rs As DAO.Recordset
    Dim strMenuCols As String      'We need to save what we've already found
    
    
    On Error GoTo ErrHandler
    
    
    strSQL = "SELECT * From [tblMenuSheet] WHERE [MenuID] = " & intDateRow     'intDateRow from Settings, global to this module
    Set rs = DBEngine(0)(0).OpenRecordset(strSQL)
    strMenuCols = ""
    strSunCol = strSunCol - 1   'Prime strSunCol (global to this module) so we can loop
    
    
    With rs
        If IsNull(!F2) Then
            MsgBox "The Fn fields are null.  Date row taken from settings is " & intDateRow
        Else
            .Edit
            For I = 1 To 7
            strFieldName = "F" & strSunCol + I             'I global to this module
            strMenuCols = strMenuCols & ";" & strFieldName
            .Fields(strFieldName) = Format(DteValue + (I - 1), "dddd, mmmm, dd, yyyy")
            Next I
            strMenuCols = Right(strMenuCols, 20)    'three per token times 7 - 1 to delete leading ";"
            !F1 = strMenuCols                       'Okay, pair the columns to menu table
            .Update
        End If
        
    
    
    
    
    End With
    
    
    rs.Close
    Set rs = Nothing
    
    
    Exit_ErrHandler:
        Exit Sub
        
    ErrHandler:
        MsgBox "Error " & Err.Number & " in Set7Days Sub : " & Err.Description, vbOKOnly + vbCritical
        MsgBox strSQL       'Let me see what the beef is?
        Resume Exit_ErrHandler
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. FindFirst in recordset where Set recordset is used
    By Miles R in forum Programming
    Replies: 19
    Last Post: 10-24-2024, 07:26 AM
  2. Dao Recordset with variable as field name?
    By Vita in forum Programming
    Replies: 9
    Last Post: 04-03-2023, 01:33 PM
  3. DAO Recordset Edit/Update
    By GraeagleBill in forum Programming
    Replies: 5
    Last Post: 06-22-2022, 01:14 AM
  4. Replies: 1
    Last Post: 05-31-2013, 08:53 AM
  5. Replies: 20
    Last Post: 06-04-2012, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums