The below code was grabbed from a thread here on Tek-Tips, but I don't remember the author....credit should be given to the creator.....
Let me know if you need some help with the syntax and usage...
' ******* Start Code *************
'Define the API calls
Private Declare Function apiSetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" _
(ByVal hwnd As Long) _
As Long
Private Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" _
(ByVal hwnd As Long, _
ByVal nCmdShow As Long) _
As Long
Public Function Connect2Database(strMDB As String, strTable As String) As Boolean
'strMDB is the full path to the database, strTable is the table name in the database
On Error GoTo ErrorHandler
Dim objAccess As Access.Application
Dim lngRet As Long
Dim db As Database
Dim rs As Recordset
If Len(Dir(strMDB)) > 0 Then
Set objAccess = New Access.Application
With objAccess
lngRet = apiSetForegroundWindow(.hWndAccessApp)
lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)
'the first call to ShowWindow doesn't seem to do anything
lngRet = apiShowWindow(.hWndAccessApp, SW_NORMAL)
.OpenCurrentDatabase strMDB
Set db = objAccess.CurrentDb
Set rs = db.OpenRecordset(strTable, dbOpenDynaset)
rs.AddNew
'this section: the first field in the table is index 0 or you can use the names...
' rs.fields(0), rs.Fields(1) or rs.Fields("Name", rs.Fields("Street"
' add as many a you need prior to the rs.Update
rs.Fields(0) = "Bird"
rs.Update
End With
End If
testme = True
ErrorExit:
objAccess.Quit
Set objAccess = Nothing
Set rs = Nothing
Set db = Nothing
Exit Function
ErrorHandler:
testme = False
Resume ErrorExit
End Function
' ********* End Code *********** Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. (Albert Einstein)
Robert L. Johnson III, MCP, Network+, A+
Access Developer/Programmer
robert.l.johnson.iii@citigroup.com
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.