Hello all,
I'm trying to create a code in excel vba that will prompt the user to browse for a .mdb file, open a user form allowing them to filter the data in 3 fields (there are 19 total fields) and drop the filtered data into excel along with all 19 of the field names.
Currently I have some of the code done. Right now it prompts the user to search for the file and populates the combo boxes with the correct data. I haven't been able to get much further.
Do all of the subs in the userform need to be private? It seems cumbersome to have to define the connection and the recordset again and again and again in each of the subs.
I do realize that the subs for the combo boxes haven't been coded yet. I'm still working on figuring all of that out.
I'm a rookie at all of this. Any help on what I'm missing would be much appreciated.
I'm trying to create a code in excel vba that will prompt the user to browse for a .mdb file, open a user form allowing them to filter the data in 3 fields (there are 19 total fields) and drop the filtered data into excel along with all 19 of the field names.
Currently I have some of the code done. Right now it prompts the user to search for the file and populates the combo boxes with the correct data. I haven't been able to get much further.
Do all of the subs in the userform need to be private? It seems cumbersome to have to define the connection and the recordset again and again and again in each of the subs.
I do realize that the subs for the combo boxes haven't been coded yet. I'm still working on figuring all of that out.
I'm a rookie at all of this. Any help on what I'm missing would be much appreciated.
Code:
Public Sub UserForm_Initialize()
ChDrive "U:\"
ChDir "U:\Engineering\DesignCommon\VALVETRAIN\SPRING DESIGN AND CALCULATION\VALKIN SPRING MODELS\01-DATA FOR CALCULATION SPECIFIC SPRINGS\SAS"
strDBpath = Application.GetOpenFilename(FileFilter:=strFilt, FilterIndex:=intFilterIndex, Title:=strDialogueFileTitle)
On Error GoTo UserForm_Initialize_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;" & "Data Source=" & strDBpath
rst.Open "SELECT DISTINCT [PartNumber] FROM [All Data Combined];", _
cnn, adOpenStatic
rst.MoveFirst
With Me.Part_Number_Box
.Clear
Do
.AddItem rst![PartNumber]
rst.MoveNext
Loop Until rst.EOF
End With
rst.Close
rst.Open "SELECT DISTINCT [PartName] FROM [All Data Combined];", _
cnn, adOpenStatic
rst.MoveFirst
With Me.Part_Name_Box
.Clear
Do
.AddItem rst![PartName]
rst.MoveNext
Loop Until rst.EOF
End With
rst.Close
rst.Open "SELECT DISTINCT [OrderNum] FROM [All Data Combined];", _
cnn, adOpenStatic
rst.MoveFirst
With Me.Order_Number_Box
.Clear
Do
.AddItem rst![OrderNum]
rst.MoveNext
Loop Until rst.EOF
End With
UserForm_Initialize_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
UserForm_Initialize_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume UserForm_Initialize_Exit
DataArray = rst.GetRows()
'show the contents of the array
Sheet1.Range("A4:Z65550").Value = DataArray
End Sub
Private Sub ComboBox1_Change()
End Sub
Private Sub ComboBox2_Change()
End Sub
Private Sub ComboBox3_Change()
End Sub
Private Sub Order_Number_Box_Change()
End Sub
Private Sub Part_Number_Box_Change()
End Sub
Private Sub OK_Button_Click()
Dim emptyRow As Long
Set Rng = Range("A1")
'Make Sheet1 active
Sheet1.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;" & "Data Source=" & strDBpath
rst.Open "FROM *[All Data Combined];", _
cnn, adOpenStatic
[]
For Each fld In rst.Fields
Rng.Value = fld.Name
Set Rng = Rng.Offset(0, 1)
Next fld
End Sub