Cursor not showing in userform textbox

BigGPL

New Member
Joined
Dec 30, 2010
Messages
15
Hi all,

Newbie to Excel 2007, and first time here.

My problem is that in a userform I have SetFocus to a specific textbox (txtGlucose) on opening, and also to any textbox(es) if no data was entered. Unfortunately, the text cursor doesn't appear, anywhere.

Any ideas?
Any help would be greatly appreciated.

Code:
Option Explicit
Dim ws As Worksheet
 
Private Sub cmdAdd_Click()
    Dim lRow As Long
    Dim MyStr
    MyStr = Time()
    'find  first empty row in database
    lRow = ActiveCell.Row
    'check for Blood Glucose Data
    If Trim(Me.txtGlucose.Value) = "" Then
        Me.txtGlucose.SetFocus
        MsgBox "Please enter Blood Glucose"
        Exit Sub
    End If
    'check for Insulin Injection Site Data
    If Trim(Me.cboLocation.Value) = "" Then
        Me.cboLocation.SetFocus
        MsgBox "Please enter Injection Site Location"
        Exit Sub
    End If
    'copy the data to the database
    Cells(lRow, 1).Value = Me.txtDate.Value
    Cells(lRow, 2).Value = Me.txtTim.Value
    Cells(lRow, 3).Value = Me.txtGlucose.Value
    Cells(lRow, 5).Value = Me.cboIns.Value
    Cells(lRow, 6).Value = Me.cboLocation.Value
    Cells(lRow, 7).Value = Me.txtNotes.Value
 
    'clear the data
    With Me
        .cboIns.Value = 37
        .cboLocation.Value = ""
        .txtDate.Value = Format(Date, "Medium Date")
        .txtNotes.Value = ""
        .txtTim.Value = MyStr
        .txtGlucose.Value = ""
        .txtDate.SetFocus
    End With
End Sub
 
Private Sub UserForm_Initialize()
    Dim cPart As Range
    Dim cLoc As Range
    Dim MyStr
    MyStr = Time()
 
    Set ws = Worksheets("LookupLists")
    With Me
        .cboLocation.List = Range("location").Value
        .cboIns.List = Range("insulin").Value
        .txtDate.Value = Format(Date, "Medium Date")
        .txtTim.Value = MyStr
        .txtNotes.Value = ""
        .cboIns.Value = 37
       ' .txtGlucose.SetFocus
    End With
    Me.txtGlucose.SetFocus
End Sub
 
 Private Sub cmdClose_Click()
    Unload Me
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this (untested)
Code:
Private Sub UserForm_Activate()
    Me.txtGlucose.SetFocus
End Sub

Remove the line of code from the Initialize Event.
 
Upvote 0
Instead of using SetFocus why not try changing the Tab Order of the controls on the form?

Right click the form and select Tab Order and you should be able to put the glucose textbox right at the top, then it will have focus when the forms opens.

Also if you want to clear the form without keeping any of the data that's been entered just close it and open it again.
Code:
Unload Me
 
UserForm1.Show ' change UserForm1 to name of form
 
Upvote 0
Hey Guys,

Thanks for the replies and advice.

The code by jaslake did the trick.

Thanks again
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top