Solved

Combobox and Textbox Change Events

Posted on 2002-05-17
2
612 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

743 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

20 Experts available now in Live!

Get 1:1 Help Now