Select Case Textbox (1 Viewer)

ClaraBarton

Registered User.
Joined
Oct 14, 2019
Messages
506
This seems very simple but doesn't work. I have 4 textboxes with 4 buttons. When a button is clicked I want to copy the textbox content to another place. The code works if I place it in each click event but I'd like to combine the code into one function so I can add more. My problem is I can't figure out how to select the button. I've used string, control... I don't know how to create a variable for the buttons to use in the first line of Select Case. Error is object variable not set.

Code:
Public Function ToNoteButton()
Dim S As String
Dim CN As String
Dim Symbol As String
Dim CD As Date
Dim ctrl As Button
CN = Nz(Me.Parent.CNotes, "")


    Select Case ctrl
        Case Me.btnVM
            CD = Nz(Me.VMDate)
            Symbol = Chr(42)
        Case Me.btnSI
            CD = Nz(Me.SIDate)
            Symbol = Chr(35)
        Case Me.btnNC
            CD = Nz(Me.NCDate)
            Symbol = Chr(37)
        Case Me.btnCT
            CD = Nz(Me.CTDate)
            Symbol = Chr(64)
    End Select
   
    If CD = 0 Then
        Exit Function
    Else: S = Symbol & CD
        If IsNull(CN) Then
            CN = S
        Else: Me.Parent.CNotes = S & "<div>" & CN
        End If
    End If
   
Me.Parent.AddToNotes

End Function
 
a control is not an event /nor value, so dont use : Select Case ctrl

for every button CLICK EVENT
send a parameter value to ToNoteButton(pvVal)

Code:
sub btnVM_click()
ToNoteButton 1
end sub


sub btnSI_click()
ToNoteButton 2
end sub



Public Function ToNoteButton(pvBtnValu)  

select case pvBtnValu
   case 1
            CD = Nz(Me.VMDate)
            Symbol = Chr(42)

   case 2
            CD = Nz(Me.SIDate)
            Symbol = Chr(35)

'etc....

end select
end sub
 
So you can't put a function in the click event?
 
Dim ctrl As Button
Select Case ctrl
Why does this not work? Because ctrl is not initialized. It is a variable, but it does not have a value.

Set ctrl = Screen.ActiveControl
Now the control has a value. Note the use of "Set" because this is an object, not a simple integer or string.

Then it would still not work, because Select Case only works with simple variables.
Select Case ctrl.Name
Now we're selecting on the name of the active control, which I hope will get you going.
 
Code:
Dim ctrl As Button

Doesn't this error? No such object as button.

Dim ctl As CommandButton
or
Dim ctl As Control
or
Dim ctl As object


You may also use

Code:
Select Case Screen.ActiveControl.Name
 
Last edited:
So you can't put a function in the click event?
You can.

It has to be a function, not a sub, and you have to include the "( )"

In the click event you'd type = MyFunction()

Screenshot 2024-03-28 113014.png


then in the function use Screen.ActiveControl

Code:
Function MyFunction() 

Select case  Screen.ActiveControl.Name

     Case "Btn1"

     Case "Btn2"

end select

End Function
 
Wouldn't this be simpler as a combobox or even an Option group? Then you are always only dealing with ONE object.
 

Users who are viewing this thread

Top Bottom