Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    djblois is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Nov 2018
    Posts
    6

    Type Mismatch when Trying to Fill Array from Field Names

    I am trying to query a table for its field names and then put those names in an array to then fill a combobox with. I have gotten the query to work but I keep getting an error when trying to populate the array.



    Here is the code I created:

    Code:
    Public Function fnGetColumnNames(table_name As String) As Variant
    
      Dim rst As New ADODB.Recordset: rst.Open "select * from  " + table_name + " where 1=0", CurrentProject.Connection
      Dim aColumnNames As Variant
      
      Dim i As Integer: For i = 0 To rst.Fields.Count - 1
        aColumnNames(i) = rst.Fields(i).name
      Next i
      fnGetColumnNames = aColumnNames
    
    
    End Function
    I keep getting a 'Type Mismatch' error on this line of code:
    Code:
    aColumnNames(i) = rst.Fields(i).name
    It is a variant and it should be accepting string values. What am I doing wrong?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Probably need ReDim statement.

    Bit why have intermediate step of filling array?

    Just build string from the recordset to use as combobox RowSource.

    Could use DAO TableDefs instead of recordset https://www.devhut.net/ms-access-vba...69e53a4fd34a7c
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Wouldn't that be Dim ColumnNames() AS Variant?

    https://docs.microsoft.com/en-us/off...claring-arrays

    However I agree, just use the Field List as the source.
    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

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    why not just set the combobox's row source type to Field List and set the rowsource to the table name.

    Click image for larger version. 

Name:	Capture.PNG 
Views:	22 
Size:	5.3 KB 
ID:	47706
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    djblois is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Nov 2018
    Posts
    6
    Is there a way to sort the list then?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Arrange fields in table design. Combobox will pull fields in order listed in table design.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    djblois is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Nov 2018
    Posts
    6
    That is what I thought; the issue is there are over 200 fields. Thank you though.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Sounds like 'spreadsheetitis' - non-normalized structure.

    Rearranging could take an hour or so but once and done. Or use VBA provided.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Is there a way to sort the list then?
    I believe you can also use a query instead of a table.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    How would a query provide a list of field names?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well I was thinking they must be held in a system table somewhere, but could not find them?
    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

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    It they are, and they must be somewhere, would have to be in a deep hidden table that cannot be exposed with the "Show system tables" option. Review https://www.access-programmers.co.uk...20c0c1bfddcd59
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I have asked isladogs, as he has extensive knowledge of the deep stuff. After all, the property sheet must get them from somewhere?
    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

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    How would a query provide a list of field names?
    Code:
    SELECT tblStates.StateID, tblStates.StateLong, tblStates.StateShort
    FROM tblStates
    heres an example using queries, a table, a procedure and a dictionary.

    The dictionary can be sorted Asc or Desc.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  15. #15
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Isn't the syntax supposed to be
    Dim aColumnNames() As Variant
    aColumnNames(0) =
    aColumnNames(1) =

    or

    Dim aColumnNames As Variant
    aColumnNames = Array(value1, value2,...)

    (or any similar construct using looping if need be).

    OP has
    Dim aColumnNames As Variant, which isn't an array, it's just a variable of type Variant. So
    aColumnNames(i) would not apply?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 07-28-2017, 06:17 PM
  2. Replies: 7
    Last Post: 08-06-2014, 05:38 PM
  3. Type Mismatch with related field
    By iacon in forum Forms
    Replies: 3
    Last Post: 05-22-2014, 06:29 AM
  4. Type mismatch from search field
    By gemadan96 in forum Programming
    Replies: 3
    Last Post: 01-09-2014, 09:29 AM
  5. Field names in an array
    By Seamus59 in forum Programming
    Replies: 11
    Last Post: 08-09-2013, 11:56 AM

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