Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > ComboBox error


Reply
 
Thread Tools Display Modes
  #1  
Old 05-14-2007, 11:58 AM
derohanes derohanes is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default ComboBox error


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:
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
Reply With Quote
  #2  
Old 05-14-2007, 12:06 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

You are confusing variables with controls. You have defined cboEditTool as a String variable but are trying to use it as a control.
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #3  
Old 05-14-2007, 01:07 PM
derohanes derohanes is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default

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?
Reply With Quote
  #4  
Old 05-14-2007, 01:20 PM
Zatriz Zatriz is offline
Centurion
 
Join Date: Aug 2003
Location: North Carolina, USA
Posts: 166
Default

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.
Reply With Quote
  #5  
Old 05-14-2007, 07:33 PM
derohanes derohanes is offline
Newcomer
 
Join Date: Jun 2006
Posts: 9
Default

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?
Reply With Quote
  #6  
Old 05-14-2007, 07:35 PM
wbeard52 wbeard52 is offline
Contributor
 
Join Date: Jul 2005
Location: Arizona
Posts: 638
Default

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
Reply With Quote
  #7  
Old 05-14-2007, 10:26 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

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)

Code:
frmEditTool.cboEditTool.AddItem rs.Fields("Dimensions")
The action of accessing a control on another form causes it to be loaded.
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->