How to Use DDE to Display Microsoft Access Data in VB 3.0ID: Q96845
|
This article demonstrates how to use DDE to obtain and display data from a
Microsoft Access database. While Microsoft Access does not support poking
data into a database, it does provide several LinkTopics, so you can get
information out from a database.
Using supported LinkTopics, you can receive:
Below you'll find example code and a detailed list of the LinkTopics
and LinkItems supported by Microsoft Access. For the most updated list
of LinkTopics and LinkItems supported by Microsoft Access, query on the
following words in the Microsoft Knowledge Base:
access and DDE and item and topic and server
System : List of supported LinkTopics.
<Database> : <Database> is the filename of an existing database.
<TableName> : <TableName> is a table within the specified database.
<QueryName> : <QueryName> is a query within the specified database.
SQL <SQL Statement> : Result of a SQL Query where <SQL Statement> is a
valid SQL expression.
SysItems - List of LinkItems supported by the System LinkTopic.
Formats - List of formats Microsoft Access can post to the clipboard.
Status - Busy or Ready.
Topics - List of all open databases.
<Macro> - Name of a macro to be executed.
TableList - List of tables
QueryList - List of queries
MacroList - List of scripts
ReportList - List of reports
FormList - List of forms
ModuleList - List of modules
<Macro> - The name of a macro to be executed.
All - All the data in the table including the column names.
Data - All rows of data without the column names.
FieldCount - Count of columns in the table or query results.
FieldNames - List of Columns.
NextRow - The next row in the table or query. When the conversation
begins, NextRow returns the first row. If the current row
is the last record, a NextRow request fails.
PrevRow - The previous row in the table or query. If PrevRow is the
first request over a new channel, the last row of the
table or query is returned. If the current row is the
first record, a PrevRow request fails.
FirstRow - Data in the first row.
LastRow - Data in the last row.
<Macro> - The name of a macro to be executed.
[db Name];TABLE <Table name>
[db Name];QUERY <Query name>
[db Name];SQL <SQL expression>;
Text1.LinkTopic = "C:\ACCESS\NWIND.MDB;TABLE Employees"
Text1.LinkTopic = "C:\ACCESS\NWIND.MDB;QUERY Sales Totals"
Text1.LinkTopic = "C:\ACCESS\NWIND.MDB;SQL Select * from Employees;"
Default Name Caption
-----------------------------------------
Command1 GetTableList Get &Table List
Command2 GetQueryList Get &Query List
Default Name FixedCols
---------------------------
Grid1 Grid1 0
The user chooses to display a table or the results of a query in Grid1.
Default Name Caption
----------------------------------
Command3 NextRow &Next Row
Command4 PrevRow &Previous Row
Const None = 0
Const Automatic = 1
Const Manual = 2
Const dbname = "C:\ACCESS\NWIND.MDB" ' Change Paths as necessary
Const accesspath = "C:\ACCESS\MSACCESS.EXE "
Sub ClearGrid ()
' Select all grid cells.
Grid1.SelStartCol = 0
Grid1.SelStartRow = 0
Grid1.SelEndCol = Grid1.Cols - 1
Grid1.SelEndRow = Grid1.Rows - 1
' Clear the cells.
Grid1.Clip = ""
' Clean up the grid.
Grid1.Col = Grid1.FixedCols
Grid1.Row = Grid1.FixedRows
Grid1.SelEndCol = Grid1.SelStartCol
Grid1.SelEndRow = Grid1.SelStartRow
End Sub
Sub PopulateGrid (IsTable%, QueryOrTable$)
If IsTable% Then
Text1.LinkTopic = "MSACCESS|" + dbname + ";TABLE " + QueryOrTable$
Else
Text1.LinkTopic = "MSACCESS|" + dbname + ";QUERY " + QueryOrTable$
End If
Text1.LinkItem = "FieldCount"
text1.linkmode = Manual
text1.linkrequest
Grid1.Cols = Val(Text1.Text)
Text1.LinkItem = "FieldNames"
Grid1.FixedRows = 0 ' Cannot additem to a fixed row
Grid1.AddItem Text1.Text, 0
Grid1.FixedRows = 1
On Error GoTo LastRowErr
Text1.LinkItem = "LastRow"
Grid1.AddItem Text1.Text, 1
Text1.LinkItem = "PrevRow"
Do
Grid1.AddItem Text1.Text, 1
Text1.LinkRequest
Loop
Exit Sub
LastRowErr:
Exit Sub ' Error occurs when last row is reached
End Sub
Sub GetList (L As ListBox, ListType$)
text2.LinkMode = Manual
text1.linkrequest
text2.LinkTopic = "MSAccess|" + dbname
text2.LinkItem = ListType$
text2.LinkMode = Automatic
StartPos% = 1
Do
Pos% = InStr(StartPos%, text2.Text, Chr$(9))
If Pos% = 0 Then Exit Do
L.AddItem Mid$(text2.Text, StartPos%, Pos% - StartPos%)
StartPos% = Pos% + 1
Loop
End Sub
Sub Form_Load ()
result% = Shell(accesspath + dbname, 1)
End Sub
Sub GetQueryList_Click ()
GetList List2, "QueryList"
End Sub
Sub GetTableList_click ()
GetList List1, "TableList"
End Sub
Sub List1_Click ()
Table$ = List1.Text
ClearGrid
PopulateGrid True, Table$
End Sub
Sub List2_Click ()
Query$ = List2.Text
ClearGrid
PopulateGrid False, Query$
End Sub
Sub NextRow_click ()
On Error GoTo NextRowErrHand:
Text1.LinkItem = "NextRow" ' Get the next row of results
text1.linkmode = manual
text1.linkrequest
Exit Sub
NextRowErrHand:
MsgBox "Last row reached"
Exit Sub
End Sub
Sub PrevRow_Click ()
On Error GoTo PrevRowErrHand
Text1.LinkItem = "PrevRow"
text1.linkmode = manual
text1.linkrequest
Exit Sub
PrevRowErrHand:
MsgBox "First Row Reached"
Exit Sub
End Sub
Additional query words: 1.00 2.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 22, 1999