03856me
Active Member
- Joined
- Apr 4, 2008
- Messages
- 297
I have written the following code with works as expected when the user presses the Add button. The data from the userform is added to the table within the workbook and then an external workbook is opened and specific textbox values are added to a table in that workbook, then it is saved and closed then the focus is back on the userform in the current workbook after clearing the textboxes.
This works perfectly the first time but then when it comes back to the userform to add another record the "Enter" function or "Tab" function to move through the textboxes won't work. At this point all I can do is click using my mouse through the fields or close the form and launch it again. Could you take a look at my code and see what I need to change to this will stop happening.....many thanks.
This works perfectly the first time but then when it comes back to the userform to add another record the "Enter" function or "Tab" function to move through the textboxes won't work. At this point all I can do is click using my mouse through the fields or close the form and launch it again. Could you take a look at my code and see what I need to change to this will stop happening.....many thanks.
Code:
Private Sub cmd_add_Click()
Dim answer As String
With txt_hrsran
If txt_hrsran.Value <> txt_hrstotal.Value Then
answer = MsgBox("Your Hours Actually Ran amount must equal the Total Hours amount for the products" & _
vbNewLine & "Please modify your input", vbOKOnly + vbCritical, "ERROR")
Exit Sub
End If
End With
If txt_total.Value > "" Then AddData
If txt_total.Value > "" Then AddDataDashboard
ClearData
End Sub
'============================================================
'MACRO TO ADD DATA
'============================================================
Private Sub AddData() ' adds data to this database
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("data")
iRow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
ws.Cells(iRow, 3).Value = Me.txt_date.Value
ws.Cells(iRow, 4).Value = "South"
ws.Cells(iRow, 5).Value = "District 1"
ws.Cells(iRow, 6).Value = Me.cbo_shift.Value
End Sub
Private Sub AddDataDashboard() ' adds data to the dashboard
Application.ScreenUpdating = False
On Error Resume Next
Workbooks("workbook1.xlsm").Activate
If Err <> 0 Then Err.Clear: Workbooks.Open Filename:= _
"C:\Users\Documents\workbook1.xlsm", _
WriteResPassword:="12345"
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("records")
iRow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
ws.Cells(iRow, 3).Value = Me.txt_date.Value
ws.Cells(iRow, 5).Value = "South"
ws.Cells(iRow, 6).Value = Me.txt_hrsran.Value
ws.Cells(iRow, 7).Value = Me.txt_hrssch.Value
Windows("workbook1.xlsm").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
Windows("OriginalWorkbook.xlsm").Activate 'Brings focus back to userform
Application.ScreenUpdating = True
End Sub