Access VBA : Getting value of textbox without focussing it.

Mil0 used Ask the Experts™
I've seen the question in some older posts but couldn't really find an answer which i like ;p

assume i have a textbox called txtUsername and a button called cmdOk.

when cmdOk gets clicked the following code should be triggered :

Msgbox "Hello " & txtUsername.text & ".", vbokonly, "Hello"

this will generate an error unless i give txtUsername focus first (txtUsername.setfocus).

the textbox is in no way related to a field in the database , it's just a plain standalone textbox.

any way to get a textbox's value without focussing it first ? (since this method kinda sucks if you need the value of 20 fields)
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I assume you are doing this in Access since this isn't a VB problem.  So, I don't know if there's another way, but I do it like this (set a local module-level variable when the textbox changes then display the variable with the Commandbutton):

Option Compare Database
Option Explicit
Dim sTextValue As String
Private Sub Command2_Click()
    MsgBox sTextValue
End Sub

Private Sub Text0_Change()
    sTextValue = Text0.Text
End Sub


ahyea , nice idea

just keeping a variable for every textbox

i was thinking of making a function like this myself :

public function gettextboxvalue(textbox as control) as string
gettextboxvalue = textbox.text
end function

but your method seems better (less load too i guess) , thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial