I have a simple Excel VBA query that returns two fields. If I run it as [Public Sub] it works fine. If it run it as [Public Function] it fails. Why would this be...what is the difference between the two?
The code below opens the connection and queries the data. Messageboxes:
1 - Shows 75 records
2 - Shows "getting ready to write out records"
3a - Third Messagebox never fires if running as a Function.
3b - If running as a SUB, then the RecordSet contents are written into cells A5:B81 (from the previous line of code), and the messagebox shows 75 records
Any thoughts? I'm fairly new to VBA/Excel Automation, but have a lot of experience elsewhere.
Thanks!
Public Function GetAccountList(cMainStart As String, cMainEnd As String)
' Check to see if data connection is open...if not, connect
' declares CON and RS. Opens Connection
OpenGLJSJ
Dim cSQL As String
Set rs = New ADODB.Recordset
Set rs.ActiveConnection = con
rs.CursorLocation = adUseClient
cSQL = "SELECT distinct Main, Sub From tblGLAccountsPeriodBalance where Main>='" & cMainStart & "' and Main<='" & cMainEnd & "'"
rs.Open cSQL, con, adOpenForwardOnly, adLockReadOnly, adCmdTxt
MsgBox rs.RecordCount
MsgBox "getting ready to write out records"
Worksheets("test").Range("a5").CopyFromRecordset rs
MsgBox rs.RecordCount
End Function