Radioactiveduck
New member
- Local time
- Today, 08:30
- Joined
- Sep 9, 2009
- Messages
- 9
Alright, so I have two forms in my access database, WIP and Exports.
The WIP form has a button cmd_ExcelExport and a textbox txt_ColNum, which is validated to be a number greater than zero. When the user pushes the cmd_ExcelExport button, I intend for Access to open the Exports form and a number of combo boxes to it equal to the number in txt_ColNum. It's also supposed to delete any combo boxes already in the Exports form before adding the new ones. This is the code for the button in the WIP form:
I placed the message box on line 37 to diagnose the problem I'm having. For some reason, whenever this code runs, if there are already combo boxes in the Exports form, it skips some of them when running through the For Each statement. For instance, if there are five combo boxes in the Exports sheet, it will always skip cmbCol2 and cmbCol4. This causes it to throw an error whenever it's trying to create new combo boxes that are supposed to use the same name.
Thoughts?
The WIP form has a button cmd_ExcelExport and a textbox txt_ColNum, which is validated to be a number greater than zero. When the user pushes the cmd_ExcelExport button, I intend for Access to open the Exports form and a number of combo boxes to it equal to the number in txt_ColNum. It's also supposed to delete any combo boxes already in the Exports form before adding the new ones. This is the code for the button in the WIP form:
Code:
Private Sub cmd_Export_Click()
'--------------------------------------------------------------
'This code opens the Exports form and adds a new combo box
'to be used for selecting a column of data from the customers
'database to be used for selecting what data to use in a client
'report.
'--------------------------------------------------------------
'--------------------------------------------------------------
'Declaring variables
'Declare and assign the database to be used
Dim db As DAO.Database
Set db = CurrentDb
'
'Declare integer and string variables
Dim ColNum As Integer, i As Integer, xLeft As Integer
Dim strFieldName As String, strVarString As String
'
'Declare and set other variables
Dim ctrl As Control
Dim xField As DAO.Field
Dim xTable As DAO.TableDef
Set xTable = db.TableDefs("Customers")
'--------------------------------------------------------------
'--------------------------------------------------------------
'Open the Exports form, Delete all combobox controls already in
'the Exports Form and add a combobox named "cmbCol1" to the form
DoCmd.OpenForm "Exports", acDesign, , , acFormEdit, acWindowNormal
'
For Each ctrl In Forms![Exports]
strVarString = ctrl.Properties("Name")
strFieldName = "Deleting " & strVarString
MsgBox strFieldName
If Left(strVarString, 6) = "cmbCol" Then DeleteControl "Exports", strVarString
Next ctrl
'
For i = 1 To txt_ColNum
strVarString = "cmbCol" & i
xLeft = (150 * (i + 1)) + (1000 * (i - 1))
Set ctrl = CreateControl("Exports", acComboBox, acDetail, , "", xLeft, 700, 1000, 300)
ctrl.Properties("Name") = strVarString
Next i
'
'Switch the Exports form to Normal view so items can be added to
'the combo box
DoCmd.OpenForm "Exports", acNormal, , , acFormEdit, acWindowNormal
'--------------------------------------------------------------
'--------------------------------------------------------------
'Set the Row Source Type property of each combo box
For i = 1 To txt_ColNum
strVarString = "cmbCol" & i
Forms![Exports].Controls(strVarString).RowSourceType = "Value List"
'
'For every column in the charter base (also known as a "Field")
'this code adds the name of that column to each combo box being
'added to the Exports Form
For Each xField In xTable.Fields
strFieldName = xField.Name
With Forms![Exports].Controls(strVarString)
.AddItem strFieldName
End With
Next xField
Next i
'--------------------------------------------------------------
End Sub
Thoughts?
Last edited: