I can do it from a Macro but aparently not from a function. below I have included some code I was using to test this. any ideas what I can do to get it to work. This would be very useful to me in many aspects.
Public Function SQLLabor3(Column1 As String)
Dim conn As Object
Dim recS As Object
Dim strQuery As String
Dim i As Long
Dim test As String
Set conn = CreateObject("ADODB.Connection")
Set recS = CreateObject("ADODB.Recordset")
'Connect to the data base
Range("C1").Select
ActiveCell.FormulaR1C1 = "Feb"
With conn
.Provider = "sqloledb"
.ConnectionString = "Data Source=CRV43;Initial Catalog=M2MDATA01;User ID=ID;Password=Password;"
.Open
End With
'Get data from Database
strQuery = "Select " & "[" & Column1 & "]" & " From labor"
recS.Open strQuery, conn
i = 2
Do Until recS.EOF
For Each fld In recS.Fields
MsgBox fld.Value
test = fld.Value
Range(“B1”) = fld.Value
Next fld
recS.MoveNext
Loop
SQLLabor3 = recS.GetRows()
'Close all connections
recS.Close
conn.Close
Set recS = Nothing
Set conn = Nothing
End Function
Bookmarks