Coleman984
Registered User.
- Local time
- Today, 12:49
- Joined
- Jul 28, 2011
- Messages
- 89
I've found many threads on the internet that have similar questions; however I have not yet been able to adapt any of other users problems to suit my needs as I am just trying to learn Access VBA, I've become decent with excel VBA but that is not helping me very much here.
The problem I've having is I'm trying to create a query with VBA when a command button is clicked; that uses several criteria. Each day should be filtered Sunday through Monday and only entries that contain 1 of 7 values should be displayed. I initially tried to use excel entirely for this project but wasn't able to get what I wanted done. And since I was basically trying to create a database with excel I decided to just use Access (using Excel still to pass data to Access; that was easy). I really don't know what else to say to make it clear what I'm trying to accomplish.
Query name is:
Query_Weekof17July2011
The database contains the following columns from left to right:
EmployeName
EmployeeID
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
I will include what I've found.
The problem I've having is I'm trying to create a query with VBA when a command button is clicked; that uses several criteria. Each day should be filtered Sunday through Monday and only entries that contain 1 of 7 values should be displayed. I initially tried to use excel entirely for this project but wasn't able to get what I wanted done. And since I was basically trying to create a database with excel I decided to just use Access (using Excel still to pass data to Access; that was easy). I really don't know what else to say to make it clear what I'm trying to accomplish.
Query name is:
Query_Weekof17July2011
The database contains the following columns from left to right:
EmployeName
EmployeeID
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
I will include what I've found.
Code:
Private Sub cmbSubmit_Click()
Dim TheDbPath As String
Dim QueryName As String
Dim SQLStr As String
'First we'll set some variables:
strSQL = "SELECT * FROM Me.cBoxTableNames.Value WHERE (Sunday = ABS)"
TheDbPath = "C:\Documents and Settings\randell.graybill\My Documents\Attendance.mdb"
QueryName = Me.cBoxTableNames.Value & " Query"
SQLStr = "SELECT * FROM " & Me.cBoxTableNames.Value
Call CreateAccessQuery(TheDbPath, QueryName, SQLStr)
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "AttendanceForm"
End Sub
Code:
Sub CreateAccessQuery(ByVal TheDbPath As String, QueryName As String, SQLStr As String)
' 1/2/06 - Carl for : Oz
' requies references : MS Active X Data Objects libs and MS Active X Ext
Dim cn As ADODB.Connection
Dim cat As ADOX.Catalog
Dim cmd As ADODB.Command
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & TheDbPath & ";"
If cn.State <> 1 Then
MsgBox ("Problem with connection")
Exit Sub
End If
Set cat = New ADOX.Catalog
Set cmd = New ADODB.Command
cat.ActiveConnection = cn
cmd.CommandText = SQLStr
On Error Resume Next
cat.Views.Append QueryName, cmd
If Err.Number = -2147217816 Then
Err.Clear
MsgBox "This Query already exists!"
End If
Set cat = Nothing
Set cmd = Nothing
cn.Close
Set cn = Nothing
End Sub