Results 1 to 10 of 10
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    Dao Recordset with variable as field name?

    I have a form with a bunch of labels that I want to update to the data of a specific field name.
    So I am trying to make a function where I can passthrough a variable and it will search a table and return the field that was passed through.

    Maybe it will be easier to show the code.



    Code:
    Public Function GCC(FieldName As String) 'GCC Stands for Get Caption Code
    Dim GCCqdf As dao.QueryDef, strSQL As String, rs As dao.Recordset
    
    
    Set GCCqdf = CurrentDb.CreateQueryDef("")
    GCCqdf.Connect = "ODBC;Driver" 'Deleted the password 
    GCCqdf.SQL = SearchSQLStr
    GCCqdf.ODBCTimeout = 1
    GCCqdf.ReturnsRecords = True
    Set rs = GCCqdf.OpenRecordset
    rs.MoveLast
    rs.MoveFirst
    
    
    strSQL = "SELECT " & TableSearch & "." & FieldName & " "
    strSQL = strSQL & "FROM " & TableSearch & " "
    strSQL = strSQL & "WHERE Key1 = '" & (Format(Key1, "00000")) & "' AND Key2 = '" & (Format(Key2, "00")) & "' AND Key3 = '" & (Format(Key3, "0000")) & "';"
    
    
    If rs.RecordCount > 0 Then
        GCC = rs! & FieldName
    Else
        GCC = "N/A"
    End If
    End Function
    (Note: TableSearch is provided by a function that runs prior to this as I have to comb through a few different tables to find where the record is first.)

    Basically How can I search a recordset by a variable name since this gives me the error "Type-declaration character does not match declared data type" and without the ampersand it searched for a non-existent field called "FieldName".

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Start by debug.print strsql and see what you have, not what you think you have.

    What line does it error on?

    If GCC= rs! then you might need Eval() ?
    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

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    I have a form with a bunch of labels that I want to update to the data of a specific field name.
    Can you explain a little more about this and about your data.
    You say that you want to update labels to data values but labels are not used to hold/show data. Labels are used to describe the data in the adjoining control. You use textbox controls to hold/show data from fields in tables/queries.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    GCC = rs.fields(FieldName)

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    If I understand correctly, you want to modify textbox label names on the form. All the data you need to do so is in the form. No need to look at tables. Copy this function code into a module and modify the literals in the Select Case to suit.
    Feed the function the form names you want changed.

    Code:
    '---------------------------------------------------------------------------------------
    ' Method : fcnFormatAllLabels
    ' Author : davegri
    ' Date   : 6/29/2021
    ' Purpose: Search form for textbox control source and change the textbox label value
    '---------------------------------------------------------------------------------------
    Public Function fcnFixFormLabels(frmName As String)
        On Error GoTo Error_Handler
        Dim ctl As Control, frm As Form
        Dim CS As String, i As Integer, LN As String, counter As Long
        DoCmd.Close acForm, frmName             'if it happens to be open
        DoCmd.OpenForm frmName, acDesign
        Set frm = Forms(frmName)
        For Each ctl In frm.Controls            'could also be {For Each ctl In frm.Section(acDetail).Controls}
            If ctl.ControlType = acTextBox Then
                counter = counter + 1
                LN = ctl.Controls(0).Caption    'this is the textbox's label value
                CS = ctl.ControlSource          'this is the control source for the textbox
                Debug.Print counter, LN, CS
            Select Case CS
                Case "DateMade"
                    ctl.Controls(0).Caption = "Creation Date"
                Case "foo"
                    ctl.Controls(0).Caption = "foo you too"
                Case Else
            End Select
            End If
        Next ctl
        DoCmd.Close acForm, frm.Name, acSaveYes
    Error_Handler_Exit:
        On Error Resume Next
        Exit Function
    Error_Handler:
        Select Case Err
            Case 2467   'ignore, textbox has no associated label
                Resume Next
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ")", vbExclamation, "Error in Function fcnFixFormLabels of FWTuner"
        End Select
        Resume Error_Handler_Exit
        Resume
    End Function
    Last edited by davegri; 04-03-2023 at 09:52 AM. Reason: clarif?

  6. #6
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Bob Fitz View Post
    Can you explain a little more about this and about your data.
    You say that you want to update labels to data values but labels are not used to hold/show data. Labels are used to describe the data in the adjoining control. You use textbox controls to hold/show data from fields in tables/queries.
    I don't believe it is too relevant but for visual purposes I am using vba to update the label.caption. I am essentially trying to write a Dlookup as a passthrough query. I have a function that finds the table and that works fine and I am trying to write a child function that returns the value of the field I passthrough on said table.

    I have a modular SQL string using the FieldName to pull specifically whatever I request.
    I just need to know how to write it out using DAO.

    Imagine if I was trying to run "rs!" through a for loop that changed to the next field name every loop. (upon rereading this sounds a bit condescending and I promise I don't mean it that way I just don't know how best to phrase it)


    Quote Originally Posted by kd2017 View Post
    GCC = rs.fields(FieldName)
    This is almost exactly what I am looking for but It doesn't find it in the collection. I think it is still searching for "FieldName". When I debug.print the string and run it in a passthrough query it returns the data I want fine.

    Quote Originally Posted by davegri View Post
    If I understand correctly, you want to modify textbox label names on the form. All the data you need to do so is in the form. No need to look at tables. Copy this function code into a module and modify the literals in the Select Case to suit.
    Feed the function the form names you want changed.
    Misunderstanding. kd2017 is closest to what I am trying to do. I explained a bit above.
    I am looking to reference a DAO recordset with a variable as the field name.

  7. #7
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Yea its definitely still searching for a field called FieldName.
    Click image for larger version. 

Name:	Capture1.PNG 
Views:	19 
Size:	3.6 KB 
ID:	50031Click image for larger version. 

Name:	Capture2.PNG 
Views:	19 
Size:	3.5 KB 
ID:	50032

  8. #8
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by kd2017 View Post
    GCC = rs.fields(FieldName)
    This actually worked but I was dumb and forgot to set the strSQL before all the searching so it was using the old search parameters and was actually not finding it in the collection.

    Thanks!

  9. #9
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    That would be a VBA interpreter bug, Im almost certain that's not the case.

    Some thoughts:
    1. Hopefully there won't be any spaces in any table or field names but if there are youll want to include square brackets when your building your sql string
    2. I could be wrong but I don't think .recordcount is reliable until one has navigated to the end of the record set.
    I would use If not (rs.BOF and rs.EOF) instead
    3. Just go ahead and alias this field name in your sql string, something like "select " & ... & " as myfield"
    Then you could refer to it as rs!myfield
    4. Or refer to fields by index number instead of strings. I don't remember the syntax off the top of my head. Review Allen Browne's elookup as an example.


    (Edit)
    While I was correct about point #2 the way you're using it in this context should work fine
    http://allenbrowne.com/ser-29.html#R...thout_MoveLast

  10. #10
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by kd2017 View Post
    That would be a VBA interpreter bug, Im almost certain that's not the case.

    Some thoughts:
    1. Hopefully there won't be any spaces in any table or field names but if there are youll want to include square brackets when your building your sql string
    2. I could be wrong but I don't think .recordcount is reliable until one has navigated to the end of the record set.
    I would use If not (rs.BOF and rs.EOF) instead
    3. Just go ahead and alias this field name in your sql string, something like "select " & ... & " as myfield"
    Then you could refer to it as rs!myfield
    4. Or refer to fields by index number instead of strings. I don't remember the syntax off the top of my head. Review Allen Browne's elookup as an example.


    (Edit)
    While I was correct about point #2 the way you're using it in this context should work fine
    http://allenbrowne.com/ser-29.html#R...thout_MoveLast
    I am more using .recordcount to tell me if there are any records because it is possible a record may not be found.
    And the issue wasn't an interpreter error it was me forgetting to setup the SQL string above the record set defining and I was assigning the parent functions SQL string to this function so it was actually pulling up an entirely different field haha.
    So the interpreter was right about not finding it in the collection.

    as for note#1 we use underscores in our tablenames. I prefer to avoid spaces in names wherever possible but will keep this in mind!
    Note 3 & 4 are good points I hadn't considered! Thank you!

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

Similar Threads

  1. RecordSet Query balking as use of variable
    By Rick_S. in forum Queries
    Replies: 9
    Last Post: 04-18-2018, 02:27 PM
  2. Problem defining variable for a recordset
    By Ast2TheRgnlMgr in forum Access
    Replies: 2
    Last Post: 08-03-2016, 03:47 PM
  3. recordset value failing on null variable
    By gangel in forum Modules
    Replies: 4
    Last Post: 08-04-2015, 05:25 AM
  4. open recordset with variable SQL
    By rivereridanus in forum Queries
    Replies: 4
    Last Post: 07-27-2011, 12:58 PM
  5. Replies: 3
    Last Post: 02-16-2010, 10:43 PM

Tags for this Thread

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