Article ID: 164922
Article Last Modified on 1/19/2007
Public oDataBase As Database Public oWorkSpace As Workspace Public oRecordSet As Recordset Public strDB As String Public strDBTable As String Dim strDBField() As String ' Array for DataBase field names.
Private Sub UserForm_Initialize()
' ********************************************
' Form initialization.
' Enter all values here for Database, Table
' and Field to use.
'
' An array is used in this example to store
' all fields used to obtain information
' from the database. The number of fields used
' must correspond to the number of columns
' in the Flexgrid control. That is, if you
' use three fields, you must have three columns.
' ********************************************
' DataBase To Use.
strDB = Options.DefaultFilePath(wdProgramPath) & _
"\Samples\Northwind.mdb"
' Database Table To Use.
strDBTable = "Customers"
' Database Field(s) To Use.
ReDim strDBField(2)
strDBField(0) = "CompanyName"
strDBField(1) = "ContactName"
strDBField(2) = "ContactTitle"
DBConnect
PopulateGridControl
End Sub
Sub DBConnect()
' ********************************************
' Initializes Jet workspace and opens a database
' ********************************************
' Establish database WorkSpace.
Set oWorkSpace = CreateWorkspace(Name:="JetWorkspace", _
UserName:="admin", Password:="", UseType:=dbUseJet)
' Open the database.
Set oDataBase = OpenDatabase(strDB)
' Set the record set to the specified table.
Set oRecordSet = oDataBase.OpenRecordset(strDBTable)
End Sub
Sub PopulateGridControl()
' ********************************************
' Populates the grid with data
' ********************************************
Dim rownum As Integer
Dim icount As Integer
' Create number of columns to equal, at least, number of fields.
If MSFlexGrid1.Cols < UBound(strDBField, 1) + 1 Then
MSFlexGrid1.Cols = UBound(strDBField, 1) + 1
End If
' Populate grid header with field names.
For icount = 0 To UBound(strDBField, 1)
MSFlexGrid1.TextMatrix(0, icount) = strDBField(icount)
Next
' Fill grid with data from field(s) in the table.
oRecordSet.MoveFirst
Do Until oRecordSet.EOF
rownum = rownum + 1
MSFlexGrid1.AddItem ""
For icount = 0 To UBound(strDBField, 1)
MSFlexGrid1.TextMatrix(rownum, icount) = _
oRecordSet.Fields(strDBField(icount))
Next
oRecordSet.MoveNext ' Move to next record in table.
Loop
End Sub
NOTE: To use the example, change the values to the full database path and
name and change the values for the table and fields that you want to use.
164815How To Create And Display A Custom Dialog Box
Additional query words: wordcon word8 word97 8.00 8.0 vb vbe vba xlvbainfo
Keywords: kbhowto kbprogramming KB164922