Is there a way to connect to a simple MDB database exclusively through code? (with no forms or data controls)
the MDB will only have a few fields on it.
If you could give me a code example it would make my day!
Printable View
Is there a way to connect to a simple MDB database exclusively through code? (with no forms or data controls)
the MDB will only have a few fields on it.
If you could give me a code example it would make my day!
Sure. Here is the DAO code to open a database and load a grid with the data in one of the tables.You will also need to go to Project>References and select the Microsoft DAO 3.51 Object Library (or the latest one you have).Code:Option Explicit
Public g_DB As Database
Private Sub Form_Load()
Dim dsLinks As Recordset
Dim intRow As Integer
Set g_DB = Workspaces(0).OpenDatabase(App.Path & "\" & "music.mdb")
Set dsLinks = g_DB.OpenRecordset("Select * from [Internet Resources] order by Description", dbOpenSnapshot)
dsLinks.MoveLast
grdLinks.Rows = dsLinks.RecordCount + 1
dsLinks.MoveFirst
Do While Not dsLinks.EOF
intRow = intRow + 1
grdLinks.row = intRow
grdLinks.col = 0
grdLinks.Text = CStr(intRow)
grdLinks.col = 1
grdLinks.Text = dsLinks!Description
grdLinks.col = 2
grdLinks.Text = dsLinks!Url
dsLinks.MoveNext
Loop
grdLinks.row = 1
End Sub
[Edited by MartinLiss on 03-19-2000 at 01:19 PM]
This is a basic example of how to open a database and recordset:
Public dbHead As Database
Dim rstHead As Recordset
Dim pstrWhere As String
Dim pstrSql As String
Dim pstrFrom As String
Dim pstrFields As String
Public Sub opendb()
'Open a Database
Set dbHead = OpenDatabase("a:/Headhunter.MDB", True)
'Open a recordset
pstrFields = " * "
pstrFrom = "From headquery"
pstrSql = "Select" & pstrFields & pstrFrom
Set rstHead = dbHead.OpenRecordset(pstrSql)
End Sub