3

I've been trying to write a piece of code that would allow me to query an SQL DB and use the returned values to populate a combobox.

The query runs fine when I ask it to return the values to the worksheet, however I don't want them there, I simply want to store them in an array to be used by the combobox.

Here's what I have so far...

Sub testQuery()
Dim varConn As String
Dim SQL As String
Dim test As String


Range("A1").CurrentRegion.ClearContents

varConn = "ODBC; DSN=Traceability DB;UID=XXX;PWD=XXX"

SQL = "Select Distinct ""Date"" from testtable"

With ActiveSheet.QueryTables.Add(Connection:=varConn, Destination:=Range("A1"), SQL:=SQL)
    .Refresh
End With

UserForm1.Show

End Sub

I'm not sure how to replace Range("A1") to an array.

Note: this is being used on the latest version of Excel for the MAC.

Thanks for the help

1
  • Don't really do much Excel programming, but doesn't Range("A1") refer to a single cell? Should it be something like Range("A1:E15")? Commented Nov 10, 2011 at 21:27

1 Answer 1

1

Save it in record set. Something like this:

Set rs = db.OpenRecordset("Select Distinct ""Date"" from testtable")

To access records in record set use GetRows Something like this:

data = rs.GetRows(j)

and then loop through data.

Sign up to request clarification or add additional context in comments.

4 Comments

I looked online and found some examples that define db as as: Dim db as Database My problem is that this datatype is not available to me for some reason, anybody know why this may be?
I'm not a VBA expert, but seems like you do not have appropriate reference. What's the exact error?
Error: User Defined type not defined. I looked at my available reference and I have the following selected: Excel, Forms, and Office Object Library. The available ones are: OLE Automation, Solver, VBA Project, Chart 14 Type Library, Microsoft Graph, Powerpoint and Word Object Library, and Visual Basic for Applications Extensibility
Take a look at this: dbforums.com/microsoft-access/… and this: bytes.com/topic/access/answers/… maybe soemthing will solve your problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.