Pookiemeister
Well-known Member
- Joined
- Jan 6, 2012
- Messages
- 643
- Office Version
- 365
- 2010
- Platform
- Windows
Is there a way to refresh my table in MS Access through an Excel userform button? After I insert data into the table I want it to requery or "RefreshAll" in the table. Is there a way to do this? Here is what I have so far. Thank you.
VBA Code:
Private Sub btnEnter_Click()
Me.lblRcrdAdded = Now()
Call ConnectDB_Insert
Unload Me
End Sub
VBA Code:
Sub ConnectDB_Insert()
On Error GoTo ErrorHandler
' Path
Dim strPath As String
' Provider
Dim strProv As String
' Connection String
Dim strConn As String
' Connection
Dim Conn As New ADODB.connection
' SQL Query
Dim strQry As String
strPath = "C:\Users\User\OneDrive\Documents\Personnel.accdb"
strProv = "Microsoft.ACE.OLEDB.12.0;"
strConn = "Provider=" & strProv & ";Data Source=" & strPath
' Connection Open
Debug.Print "Opening connection..."
Conn.Open strConn
Debug.Print "Connection opened."
' SQL Query with parameters
strQry = "INSERT INTO Personnel ([Employee #], [Network ID], [First Name], [Last Name], [Email Address], [Phone], [Record Added]) " & _
"VALUES (me.txtempnum.text,me.txtntwrkid.text , me.txtfn.text, me.txtln.text, me.txtemail.text, me.txtphone.text, me.lblrcrdadded.caption)"
' Create a command object
Dim cmd As New ADODB.Command
With cmd
.ActiveConnection = Conn
.CommandText = strQry
.CommandType = adCmdText
' Add parameters
.Parameters.Append .CreateParameter("EmployeeNum", adVarChar, adParamInput, 255, frmPersonnelEntry.txtEmpNum.Text)
.Parameters.Append .CreateParameter("NetworkID", adVarChar, adParamInput, 255, frmPersonnelEntry.txtNtwrkID.Text)
.Parameters.Append .CreateParameter("FirstName", adVarChar, adParamInput, 255, frmPersonnelEntry.txtFN.Text)
.Parameters.Append .CreateParameter("LastName", adVarChar, adParamInput, 255, frmPersonnelEntry.txtLN.Text)
.Parameters.Append .CreateParameter("EmailAddress", adVarChar, adParamInput, 255, frmPersonnelEntry.txtEmail.Text)
.Parameters.Append .CreateParameter("Phone", adVarChar, adParamInput, 255, frmPersonnelEntry.txtPhone.Text)
.Parameters.Append .CreateParameter("RecordAdded", adVarChar, adParamInput, 255, frmPersonnelEntry.lblRcrdAdded.Caption)
End With
' Execute the command
Debug.Print "Executing command..."
cmd.Execute
Debug.Print "Command executed."
' Refresh the form
'frmPersonnelEntry.Requery
' Close the connection
Conn.Close
Set Conn = Nothing
Set cmd = Nothing
' SQL Query to refresh the table
Call RefreshAccessTable
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
If Not Conn Is Nothing Then
Conn.Close
Set Conn = Nothing
End If
Set cmd = Nothing
End Sub
VBA Code:
Sub RefreshAccessTable()
On Error Resume Next
'Path
Dim strPath As String
'Provider
Dim strProv As String
'Connection String
Dim strConn As String
'Connection
Dim Conn As New ADODB.connection
'SQL Query
Dim strFrmQryEID As String
'RecordSet
Dim rs As New ADODB.Recordset
strPath = "C:\Users\User\OneDrive\Documents\Personnel.accdb"
strProv = "Microsoft.ACE.OLEDB.12.0;"
strConn = "Provider=" & strProv & "Data Source=" & strPath
Conn.Open strConn
' Debug: Print the SQL query
Debug.Print "SQL Query: " & strFrmQryEID
rs.Open strFrmQryEID, Conn
rs.Requery
rs.Close
Conn.Close
' Notify user
MsgBox "All linked tables have been refreshed.", vbInformation
End Sub