Solved

Combobox and Textbox Change Events

Posted on 2002-05-17
2
613 Views
Last Modified: 2012-05-04
I'm using VB6 against an SQL Server database.
I'm having a minor problem and I think it has to do with the sequencing of my code.
I have a textbox, combobox, and a datagrid.
All are blank when the form loads.
The textbox will contain a code, the combobox to the right will contain a description and the grid below will be populated with the data from this code and/or description.
The user can enter a code in the textbox or choose a selection from the combobox to poulate the grid.
I'm having a problem getting them in sync.

When the user enters a value in the textbox, that part works:  that value is searched and a description is brought back in the combobox and the grid populated.

But, when I search from the combobox, the textfield is filled correctly and the grid filled correctly but then the combobox is cleared - it's not set to the correct description.
When I step thru the debugger, I see where my combobox is getting cleared but I can't seem to get the code right.
--------------------------------------------------------
Here's the code from my combobox_click event and then the code for the txt_change event if that will help.
DataInquire is the function that populates the grid.
----------------------------------------------------
Private Sub cmbAgencyCode_Click()
Dim Done As Boolean
    Done = False
    If Not (rsAgencies.BOF And rsAgencies.EOF) Then
        rsAgencies.MoveFirst
        While Not rsAgencies.EOF And Not Done
            If rsAgencies.Fields("description") = cmbAgencyCode.Text Then
                txtAgency.Text = rsAgencies.Fields("code")
                Done = True
            End If
            If Not Done Then rsAgencies.MoveNext
        Wend
    End If
       
    DataInquire
    txtMessage = "Press ADD button or Double click on item you want to MODIFY or DELETE"
    cmbAgencyCode = UCase(cmbAgencyCode)
End Sub
---------------------------------------------------------
Private Sub txtAgency_Change()
    If Len(txtAgency) = 5 Then
        Call txtAgency_Validate(False)
        If txtAgency = "" Then Exit Sub
    End If
End Sub
---------------------------------------------------------
Private Sub txtAgency_Validate(Cancel As Boolean)
Dim Done As Boolean
    txtAgency = UCase$(txtAgency)
    cmbAgencyCode.Clear
    rsAgencies.MoveFirst
        While Not rsAgencies.EOF
            cmbAgencyCode.AddItem rsAgencies!Description
            rsAgencies.MoveNext
        Wend
    If txtAgency <> "" Then
        rsAgencies.MoveFirst
        Done = False
        While Not rsAgencies.EOF And Not Done
            If txtAgency = rsAgencies.Fields("code") Then
                cmbAgencyCode.Text = rsAgencies.Fields("description")
                Cancel = False
                Done = True
            Else
                Cancel = True
            End If
            If Not Done Then rsAgencies.MoveNext
        Wend
        If Cancel = True Then
            MsgBox "Invalid Agency Code"
            txtAgency = ""
            txtAgency.SetFocus
        End If
    End If
End Sub
---------------------------------------------------------


Thanks in Advance.
John
0
Comment
Question by:jtrapat1
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 7016317
Change your click event code to this and it will all run smoothly:

Private Sub cmbAgencyCode_Click()
Dim Done As Boolean
   Done = False
   Dim intL As Integer
   If Not (rsAgencies.BOF And rsAgencies.EOF) Then
       rsAgencies.MoveFirst
       While Not rsAgencies.EOF And Not Done
           If rsAgencies.Fields("description") = cmbAgencyCode.Text Then
               txtAgency.Text = rsAgencies.Fields("code")
               Done = True
            Else
             intL = intL + 1
           End If
           If Not Done Then rsAgencies.MoveNext
       Wend
   End If
   cmbAgencyCode.ListIndex = intL
'   DataInquire
'   txtMessage = "Press ADD button or Double click on item you want to MODIFY or DELETE"
'   cmbAgencyCode = UCase(cmbAgencyCode)
End Sub
0
 

Author Comment

by:jtrapat1
ID: 7017008
TimCottee,

Thanks for the help; that fixed the problem.
Related to this, there has been something that is bugging me about this project and maybe you could answer it.
The combobox I'm using will not recognize the _Change() Event.  

I tried different styles such as Dropdown List and Dropdown Combo but it still will not work.
I checked thru all of the properties but none of them were disabled to cause this problem.
Do you have any idea why this event would be disabled?

I've had to use the _Click event to take the place of the _Change event.

Thanks
John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now