VBA sending string error

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,413
Some SQL reading from access


VBA Code:
Function ReadFromAccess2(sqlString As String, fieldName As String) As String
...

Code:
Sub Populate_from_Current_Invoice()

    Dim fieldName As String
    Dim tableName As String
    Dim custID As Integer
    Dim sqlString As String
    Dim result As String
 
    ' Set the field name, table name, and customer ID
    tableName = "Customer"
    custID = 5346  ' Example customer ID, you can set this dynamically as needed
      

'this works
    fieldName = "City"
    Range("A1") = ReadFromAccess2("SELECT " & fieldName & " FROM " & tableName & " WHERE Custid=" & custID, fieldName)
 
'this does not
     Range("A1")  = ReadFromAccess2("SELECT " & fieldName & " FROM " & tableName & " WHERE Custid=" & custID, "City")

I plan on using the a lot and would like the above 1-liner to work. (Error: Item cannot be found in the collection corresponding to the requested name or ordinal.)

Any Ideas?
-Ross
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
ReadFromAccess2 is your function and you haven't shown the code for it, so we have no idea how it is using the fieldName argument.

Theoretically passing in a string literal should work the same as passing a String variable with the same value, but to diagnose this we have to see all of the code in ReadFromAccess2, plus any other Subs or Functions that it calls. For example, does ReadFromAccess2 try to change the value of fieldName?
 
Upvote 0
sorry for delay,

entire code for for the function below.

VBA Code:
Function ReadFromAccess2(sqlString As String, fieldName As String) As String 'this will return a single record field to a cell

    On Error GoTo ErrorHandler
   
    Dim conn As Object
    Dim rs As Object
    Dim connectionString As String
    Dim result As String
   
    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ross\Desktop\CHat GPT Code\Invoice.accdb;"
   
  
    Set conn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
   
    conn.Open connectionString
    rs.Open sqlString, conn
   
    If Not rs.EOF Then
        If IsNull(rs.Fields(fieldName).Value) Then
            result = ""
        Else
            result = rs.Fields(fieldName).Value
        End If
    Else
        result = ""
    End If
   
    rs.Close
    conn.Close

    Set rs = Nothing
    Set conn = Nothing
   
    ReadFromAccess2 = result
    Exit Function
   
ErrorHandler:
    result = "Error: " & Err.Description
    ReadFromAccess2 = result
End Function
 
Upvote 0
I don't see anything in that code that would cause this error. When the error occurs, does debug mode show you what line of code causes the error? If so which one? If not I would step through the code with F8 to pin down exactly where the error is occurring. However, that may or may not help.

But again, from what I've seen so far, theoretically this error shouldn't occur. Unfortunately without your Access database I'm unable to test it myself.
 
Upvote 0
I set up the data in Access and using that exact same code it works fine for me which can confirms Jeff's theory that the code looks fine.
I had thought that using fieldName for different purposes might have caused an issue but it doesn't seem to be impacting it.
I also though maybe it needed ByVal especially since fieldName is being used by the main procedure for one thing but in the function for something else but it doesn't seem to make any difference either.
Function ReadFromAccess2(sqlString As String, ByVal fieldName As String) As String
 
Upvote 0
omg,
I think i found the problem.

I was using fieldName 2 times.

VBA Code:
Range("A1")  = ReadFromAccess2("SELECT " &  fieldName & " FROM " & tableName & " WHERE Custid=" & custID, "City")

needs to be
VBA Code:
Range("A1")  = ReadFromAccess2("SELECT " & "City" & " FROM " & tableName & " WHERE Custid=" & custID, "City")

Sorry for wasting everyone time.

-Ross

 
Upvote 0
I cannot see an immediate problem and also am a bit confused which part throws an error.
However - one suggestion:
You only select one record and one field. So you don't need the field's name as a separate argument.
you can use this:
VBA Code:
rs.Fields(0).Value
Basically ReadFromAccess2 needs only the SQL string as an argument
 
Upvote 0
omg,
I think i found the problem.
Oh, I see. Yes, in the second example that doesn't work, it is using fieldName but it omits setting it
VBA Code:
    fieldName = "City"
so that fieldName is the null string. As many times as I read this, I didn't realize it.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top