ComboBox error

derohanes
05-14-2007, 12:58 PM
I am curious about what is expected of the combo control. I am using cbo variable to load the control. The question I have is in the declaration of the control. I have successfully loaded a combo box with data from a db using .additem after the cbo variable without declaring it. I forgot to dimension that, but it works. I don't understand why though. In another sub I use another cbo variable and I get errors. If I don't DIM it I get a "Variable not defined error" if I do DIM it I get an "Invalid qualifier" error. Any ideas?

I am trying to load the combo box from an Access DB then using the selection to pull that record. In the section I am having problems with, "Private Sub cmdEditToolForm_Click()", "cboEditTool.additem" is the problem. In the "Private Sub Form_Load()" section of the code, "cboToolSizeFrom.AddItem" has no problems.

I am incrementally adding buttons as I develop application. It worked through to the point of reading the DB, and adding to the DB, but I'm having trouble reading in the section where I want to edit.

This is partial code, to save space, but the application has a main form, from which I select a tool size, and do some calculations on it. That main form can launch 2 others using command buttons. One adds to the DB, which works. The other should load the combo box with available tools in the DB, I select a tool and it populates boxes for editing. That portion is not complete, because I'm stuck on populating the combo box because of this error.

Code:
Option Explicit
Public booIsAdding As Boolean
Public cn As ADODB.Connection
Public rs As ADODB.Recordset
Dim strSQL As String
'Test portion*************
Dim txtTestSize As String
Dim sngTestArea As Single
Dim sngTestLength As Single
Dim strToolInitial As String
Dim strToolTarget As String
Dim strEditToolDimension As String, strEditShape As String, strSQL_Insert As String, strEditHobNumber As String
Dim sngEditCupDepth As Single, sngEditCupVolume As Single, sngEditDieArea As Single
Dim sngEditCupSurfaceArea As Single
Dim cboEditTool As String
Dim strAddToolDimension As String
Dim sngAddCupDepth As Single, sngAddCupVolume As Single, sngAddDieArea As Single
Private Sub cmdAddToolForm_Click()
frmAddTool.Left = (Screen.Width - frmAddTool.Width) * 2 / 3
frmAddTool.Top = (Screen.Height - frmAddTool.Height) * 2 / 3
frmAddTool.Show
End Sub
Private Sub cmdEditToolForm_Click()
'On Error GoTo errHandler_DB
strSQL = "SELECT * FROM tblSizes"
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=tooling.mdb"
cn.Open
Set rs = New ADODB.Recordset 'as we did with the connection
rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
rs.MoveFirst 'moves to the first record
Do Until rs.EOF = True 'this is the Loop to add items to the combo box
cboEditTool.AddItem rs.Fields("Dimensions") 'this adds items from field1 into the combo box
rs.MoveNext 'moves next record
Loop
rs.MoveFirst
fillfieldDimensions 'i'll explain this later on.
Me.MousePointer = 0 'sets the mouse pointer to the normal arrow
rs.Close
frmEditTool.Left = (Screen.Width - frmEditTool.Width) * 2 / 3
frmEditTool.Top = (Screen.Height - frmEditTool.Height) * 2 / 3
frmEditTool.Show
End Sub

Private Sub Form_Load()
reset
frmToolingTest.Left = (Screen.Width - frmToolingTest.Width) / 2
frmToolingTest.Top = (Screen.Height - frmToolingTest.Height) / 2
strSQL = "SELECT * FROM tblSizes"
Me.MousePointer = 11 'this makes the mouse pointer the hourglass
Set cn = New ADODB.Connection 'we've declared it as a ADODB connection lets set it.
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=tooling.mdb"
cn.Open
Set rs = New ADODB.Recordset 'as we did with the connection
rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
rs.MoveFirst 'moves to the first record
Do Until rs.EOF = True 'this is the Loop to add items to the combo box
cboToolSizeFrom.AddItem rs.Fields("Dimensions") 'this adds items from field1 into the combo box
cboToolSizeTo.AddItem rs.Fields("Dimensions") 'this adds items from field1 into the combo box
rs.MoveNext 'moves next record
Loop
rs.MoveFirst
fillfieldDimensions 'i'll explain this later on.
Me.MousePointer = 0 'sets the mouse pointer to the normal arrow
rs.Close
End Sub

DougT
05-14-2007, 01:06 PM
You are confusing variables with controls. You have defined cboEditTool as a String variable but are trying to use it as a control.

derohanes
05-14-2007, 02:07 PM
I first had it without a DIM, but had a problem, that's when I tought it my have been a problem with declaration. That explain why I'm not getting an error with cboToolSizeFrom.AddItem, but why am I getting a "Variable not defined error" with cboEditTool.AddItem?

Zatriz
05-14-2007, 02:20 PM
That's what DougT was talking about. cboEditTool is declared as a String and you are trying to use it as a ComboBox. If you have a ComboBox on your form named cboEditTool than remove the Dim for cboEditTool.

derohanes
05-14-2007, 08:33 PM
I understand. I should not DIM a combobox. The problem is I get an error when I remove it. Don't look at the code above as the only version I tried. As I mentioned even without the DIM, I get "Variable not defined error". I think it might be the way I am, or maybe not, loading the form. cboEditTool is a combobox in frmEditTool. I have tried the above code with the frmEditTool.Show in front of the ADO code, but that doesn't help either. I am trying to fill the combobox with the contents of the DB field "Dimensions" so that I can select it when the form is displayed. I might not be loading the form at all, and that is why cboEditTool is not working. If so, how do I program around that?

wbeard52
05-14-2007, 08:35 PM
make sure at the top of your form you have the words

Option Explicit

This will ensure that all the variables are at least dimmed in someway, albeit they may be dimmed incorrectly

DougT
05-14-2007, 11:26 PM
It looks as if you're attempting to access a control on one form from another form. In that cae you have to fully qualify the control by preceding it with the form's name (as you have done in the Form_Load code)


frmEditTool.cboEditTool.AddItem rs.Fields("Dimensions")

The action of accessing a control on another form causes it to be loaded.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum