View Single Post
 
Old 02-14-2017, 09:24 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 51
Unhappy Code not executing

Hi All,

I'm using Visual Studio 2015 Community Version

I have a form which has fields for data entry. These consist of textbox, combobox and listbox controls.

Initially just one textbox is enabled where the user enters a product number.
The validated method of the textbox checks the status of the entered number and either allows the user to proceed or gives an appropriate warning.

When a warning is generated a sub called "refreshfields" is called which clears all the fields and and as result resets the enabled status of the controls ready for a fresh number.

The first problem I have is that, following a warning message, the "refreshfields" sub is not always called. Stepping through the code it doesn't go to the "refreshfields" routine.

Code:
   Private Sub txtNumber_Validated(sender As Object, e As EventArgs) Handles txtNumber.Validated
        Dim sStatus As String
        txtNumber.Text = UCase(txtNumber.Text)
        If Len(txtNumber.Text) > 0 Then
            sStatus = GetStatus(txtNumber.Text) 'function to check the status which returns a string
            Select Case sStatus
                Case "0"
                    MsgBox("Warning message", vbCritical)
                    Call RefreshFields() 'stepping through, code line is executed by doesn't go to the routine
Secondly, on the occasions where "refreshfields" is entered successfully (it's called form other events also such as clicking a Clear All button), my loop to clear the controls doesn't have any effect even though it appears to execute without error.
So, after executing the below, textboxes still contain text etc.
Code:
    Sub RefreshFields()
        Dim ctrl As Control
        Me.Cursor = Cursors.Default
        Me.Enabled = True
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is ComboBox Then
                ctrl.ResetText()
            ElseIf TypeOf ctrl Is TextBox Then
                ctrl.Text = vbNullString
            ElseIf TypeOf ctrl Is ListBox Then
                ctrl.ResetText()
            End If
        Next        
    End Sub
Any suggestions much appreciated.
Reply With Quote