Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Connecting to an Access Database
Message
From
15/04/2003 21:05:31
 
 
To
15/04/2003 16:00:21
Sony Joseph
Dovenmuehle Mortgage Inc
Schaumburg, Illinois, United States
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00778022
Message ID:
00778153
Views:
43
>Hi,
>
>Can any one help me with code snippet to connect to a local Access Database through VFP?
>
>TIA.

Here are a couple of reasonably simple functions to do it. See if they get you started: You'll need a file called adovfp.h which you can get by downloading rs2dbf.exe from the MS website.

Note that the second function returns an ADO recordset not a vpf cursor. There are other ways to do this, but as I said, this is fairly simple and workable.
Function OpenAccessConnection
LParameters tcMdb, tcID, tcPwd  && path to access mdb, ID and Password
Local loRetVal, lcDir, lcDatabase, lcConnString

#Include 'adovfp.h'

If PCount() < 3
   tcPwd = ''
endif
If PCount() < 2
   tcId = ''
Endif

lcDir = JustPath(tcMdb)   && find path to MDB
lcDatabase = JustStem(tcMdb)  && separate out the mdb name

If !Empty(lcDatabase) And (PCOUNT() = 3)
   lcConnString = "Driver={Microsoft Access Driver (*.mdb)};DefaultDir=" + lcDir + ;
                  ";DBQ=" + lcDatabase + ;
                  ";User Id=" + tcID + ;
                  ";Password=" + tcPwd
   loRetVal = CreateObject('ADODB.Connection')
   loRetVal.Open(lcConnString)
Else
   loRetVal = .NULL.
Endif

Return loRetVal



Function OpenAccessTable
LParameters toConnection, tcTable, tcHow, tcWhere  && connection object from above, 
     * tablename or select statement ,
     * how to connect (optimistic etc., cursor location (server or client)
Local loRetVal, lnHow, lnWhere, lcOpenString

#Include 'adovfp.h'

If PCount() < 4
   lnWhere = adUseClient
Else
   If Upper(Alltrim(tcWhere)) = 'SERVER'
      lnWhere = adUseServer
   Else
      lnWhere = adUseClient
   Endif
Endif

* default to readonly
If PCount() < 3
   lnHow = adLockReadOnly
Else
   Do Case
      Case Upper(Alltrim(tcHow)) = 'OPTIMISTIC'
         lnHow = adLockOptimistic

      Case Upper(Alltrim(tcHow)) = 'PESSIMISTIC'
         lnHow = adLockPessimistic

      Case Upper(Alltrim(tcHow)) = 'BATCH'
         lnHow = adLockBatchOptimistic

      Otherwise
         lnHow = adLockReadOnly

   EndCase
Endif

loRetVal = .Null.  && return null if this doesn't work

If (PCount() >= 2) And !IsNull(toConnection) And !Empty(tcTable)
   If toConnection.State = adStateOpen
      * create open string using a select - either sent in, or created here
      lcOpenString = IIF('SELECT ' $ upper(tcTable), ;
         Alltrim(tcTable), 'Select * from ' + Alltrim(tcTable))
      loRetVal = CreateObject('ADODB.RecordSet')
      loRetVal.CursorLocation = llnWhere
      loRetVal.Open(cOpenString, toConnection, adOpenStatic, lnHow, adFetchAsync + adCmdText)
   Endif
Endif

Return loRetVal
Previous
Reply
Map
View

Click here to load this message in the networking platform