Hello! I have been so close to getting this new feature for my UserForm completed but I am running into an issue with my Me.Controls. When testing out my search function with the UserForm by hitting Enter, it gives me a Run-time error about not being able to find the specified object. I am working within a data table, so I have it as Me.Controls("SunSch" & i) which is my ListObject. I may just be thinking about this two hard, but I just can't seem to figure it out. Any assistance would greatly be appreciated.
VBA Code:
Option Explicit
Dim fnd As Range
Enum XLRecordActionType
xlGetRecord
xlUpdateRecord
xlClearForm
End Enum
Private Sub UserForm_Initialize()
Me.NewStoreTextBox.SetFocus
Me.UPDATE.Enabled = False
End Sub
Private Sub UPDATE_Click()
GetUpdateRecord xlUpdateRecord
End Sub
Private Sub NewStoreTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then Findit
End Sub
Private Sub NewStoreTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = fnd Is Nothing
End Sub
Private Sub Findit()
Dim i As Integer
Dim SEARCH As String
Dim sh As Worksheet
Set sh = Sheet1
SEARCH = Me.NewStoreTextBox.Text
If Len(SEARCH) = 0 Then Exit Sub
Set fnd = sh.Columns("A:A").Find(SEARCH, , , xlWhole)
If Not fnd Is Nothing Then
GetUpdateRecord xlGetRecord
Else
MsgBox "Store Number Not Found", 48, "Input Error"
End If
End Sub
Sub GetUpdateRecord(ByVal Action As XLRecordActionType)
Dim i As Integer
For i = 1 To 31
With Me.Controls("SunSch" & i)
If Action = xlGetRecord And i > 1 Then .Text = fnd.Offset(, i - 1).Text
If Action = xlUpdateRecord Then fnd.Offset(, i - 1).Value = .Text
If Action <> xlGetRecord Then .Text = ""
If i = 1 Then .SetFocus
End With
Next i
Me.UPDATE.Enabled = CBool(Action = xlGetRecord)
If Action <> xlGetRecord Then Set fnd = Nothing
End Sub