Results 1 to 3 of 3

Thread: Database - without using visual controls - possible?

  1. #1
    Guest
    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!

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Sure. Here is the DAO code to open a database and load a grid with the data in one of the tables.
    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
    You will also need to go to Project>References and select the Microsoft DAO 3.51 Object Library (or the latest one you have).

    [Edited by MartinLiss on 03-19-2000 at 01:19 PM]

  3. #3
    Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    35

    short answer - yes

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width