Article ID: 149581
Article Last Modified on 11/23/2006
Sub OneDimArray()
' Open a channel to Microsoft Query using DDE.
Chan = DDEInitiate("MSQUERY", "System")
' Enable a user to build his or her own query in Microsoft Query and
' Exit Microsoft Query using "Return to Excel" on the File Menu.
DDEExecute Chan, "[UserControl('&Return to Excel',3,true)]"
' Return the request item desired. In this example you return
' the name of the Data Source in use by the active query.
MsgBox DDERequest(Chan, "DataSourceName")(1)
' Exit Microsoft Query if this is the only Query open. If other
' queries are open and you want to close them all, use
' "[Exit(True)]".
DDEExecute Chan, "[Exit(False)]"
' Terminate the DDE channel.
DDETerminate Chan
End Sub
Sub TwoDimArray()
' Open a channel to Microsoft Query using DDE.
Chan = DDEInitiate("MSQUERY", "System")
' Enable a user to build his or her own query in Microsoft Query and
' Exit Microsoft Query using "Return to Excel" on the File Menu.
DDEExecute Chan, "[UserControl('&Return to Excel',3,true)]"
' Return the request item desired. In this example you return
' the names of all remote databases available in the ODBC Data
' Sources.
LogArray = DDERequest(Chan, "Logon")
' Determine how many elements are in the first dimension of the
' returned array. The second dimension of the array will always be
' one.
LogLen = UBound(LogArray, 1)
' Set up a loop to display each database name.
For i = 1 To LogLen
' Display each database name in a message box.
MsgBox "Logon function - Returns the ODBC.INI Data Source" _
& "Connections" & Chr(10) & Chr(10) & "Logon Connection " _
& i & ": " & LogArray(i, 1)
' Return to "For I" above until all database names have been
' displayed.
Next i
' Exit Microsoft Query if this is the only Query open. If other
' queries are open and you want to close them all, use
' "[Exit(True)]".
DDEExecute Chan, "[Exit(False)]"
' Terminate the DDE channel.
DDETerminate Chan
End Sub
Sub MultiDimArray()
' Open a channel to Microsoft Query using DDE.
Chan = DDEInitiate("MSQUERY", "System")
' Enable a user to build his or her own query in Microsoft Query and
' Exit Microsoft Query using "Return to Excel" on the File Menu.
DDEExecute Chan, "[UserControl('&Return to Excel',3,true)]"
' Return the request item desired. In this example we are returning
' the names of all the currently connected remote databases.
OffArray = DDERequest(Chan, "Logoff")
' Determine how many elements are in the first dimension of the
' array. The second dimension of the array will always be one.
OffLen = UBound(OffArray, 1)
' If the first dimension of the array contains only one element...
If UBound(OffArray) = 1 Then
' Then display the database name in a message box.
MsgBox "Logoff function - Returns the currently" _
& " connected database name" & Chr(10) & Chr(10) _
& "Database: " & OffArray(1)
' However, if there is more than one value in the first dimension
' of the array...
Else
' Then loop through all of the values returned.
For i = OffLen To 1 Step -1
' And display each one in a message box. In this case, we are
' displaying the most recently accessed database name first.
MsgBox "Logoff function - Returns all currently" _
& " connected remote databases" _
& Chr(10) & Chr(10) & "Connected Database " _
& i & ": " & OffArray(i, 1)
' Return to "For I" above until all database names have been
' displayed.
Next i
' End the Block If statement.
End If
' Exit Microsoft Query if this is the only Query open. If other
' queries are open and you want to close them all, use
' "[Exit(True)]".
DDEExecute Chan, "[Exit(False)]"
' Terminate the DDE channel.
DDETerminate Chan
End Sub
Sub TableArray()
' Open a channel to Microsoft Query using DDE.
Chan = DDEInitiate("MSQUERY", "System")
' Enable a user to build his or her own query in Microsoft Query and
' Exit Microsoft Query using "Return to Excel" on the File Menu.
DDEExecute Chan, "[UserControl(' &Return to Excel' ,3,true)]"
' Return the request item desired. In this example we are returning
' a table of values that describe the attributes of the database data
' result.
FieldArray = DDERequest(Chan, "FieldDef")
' Turn on error handling. In this case, you want to resume with the
' next line of code if an error is detected.
On Error Resume Next
' You can now use "IsError" to test to see if trying to access the
' second dimension will produce an error.
If IsError(Fieldcols = UBound(FieldArray, 2)) Then
' If you get an error, suspect that there is only one dimension
' in the array (other errors could also occur) and set the number
' of field rows to 1 and the field column count to 5. This code does
' not address any other error condition.
Fieldrows = 1
Fieldcols = UBound(FieldArray, 1)
' Once you pass this point, you want turn off the error
' handler (unless you want to write code to handle other
' potential errors)
On Error GoTo 0
' If there was no error, then this is a two dimension array.
Else
' Get the number of records from the first dimension.
Fieldrows = UBound(FieldArray, 1)
' Get the number of columns from the second dimension.
Fieldcols = UBound(FieldArray, 2)
' End the Block If statement.
End If
' Resize the worksheet range for the number of rows and columns
' in the table and load the data onto the worksheet.
Worksheets("Sheet1").Range("A1").Resize(Fieldrows, Fieldcols) = _
FieldArray
' Exit Microsoft Query if this is the only Query open. If other
' queries are open and you want to close them all, use
' "[Exit(True)]".
DDEExecute Chan, "[Exit(False)]"
' Terminate the DDE channel.
DDETerminate Chan
End Sub
Additional query words: 5.0 5.0c 5.00a 5.00c
Keywords: kbcode kbhowto kbprogramming KB149581