Message Box Appears before all user form records have been uploaded

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
185
Office Version
  1. 365
Platform
  1. Windows
Not a major problem but in a workbook I have two worksheets each of which have similar userforms. The structure code for the uploading of new records is the same for both forms and in each case the code instructs a message box to pop up once a record has been added.

Curiously, the second worksheet only adds the first record in the user form before the message box appears and the remaining records only get added once the message box is acknowledged.

However, if I remove the message box, then all the records get added.

I have a similar issue when I update records

Input Record Message Box Code - appearing before all records have been added
MsgBox txtPLBRNumber & " has been added to the Preserved Locomotive database", 0, "Stock Number Added"

Input Record Message Box Code - appearing after all records have been added
MsgBox txtStockNumber & " has been added to the database", 0, "Stock Number Added"

Whilst this is not critical, it is something that I would like to understand what might be causing this to happen.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Just showing the MsgBox line of code is not enough to diagnose this. Please show all the code for the UserForm with the problem. (I suggest you use code tags when pasting code. You can select the code then click the VBA button to mark the code. This will preserve the original spacing in your code to help readability.)
 
Upvote 0
Just showing the MsgBox line of code is not enough to diagnose this. Please show all the code for the UserForm with the problem. (I suggest you use code tags when pasting code. You can select the code then click the VBA button to mark the code. This will preserve the original spacing in your code to help readability.)
Hi Jeff

Here is the entire code for the User Form.

As I mentioned, the code for adding and updating details are exactly the same as for another user form in a seprate worksheet in the same workbook. The only exception is that the source and range details have been changed to provide the corresponding details. Details from the other user form are all inputted into the worksheet before the message box appears.

With this code, only the first detail Cells(lastrow + 1, "A").Value = cboPLType are uploaded into the worksheet before the message box appears and the remaining details are only uploaded once the message box is acknowledged. This happens for both the Add and Update commands.

I have used the same structured code to add and update informstion in a number of user forms and files and this is the only form where this occurs.

Any help would be most welcome

User Form Code


Dim Currentrow As Long

Private Sub FullDetails_Click()

End Sub

Private Sub UserForm_Initialize()

cboPLType.Value = ""
txtPLClass.Value = ""
txtPLSubClass.Value = ""
txtPLBRNumber.Value = ""
txtPLCurrentNumber.Value = ""
txtPLPreviousNumbers.Value = ""
txtPLStockName.Value = ""
DTPicker2.Value = ""
txtPLStockLocation.Value = ""
txtPLBRNumberSearch.Value = ""
txtPLCurrentNumberSearch.Value = ""

txtPLClass.SetFocus

End Sub
Private Sub txtPLBRNumber_Change()
'Converts text to upper case
txtPLBRNumber.Text = UCase(txtPLBRNumber.Text)

End Sub
Private Sub txtPLCurrentNumber_Change()
'Converts text to upper case
txtPLCurrentNumber.Text = UCase(txtPLCurrentNumber.Text)

End Sub
Private Sub txtPLPreviousNumbers_Change()
'Converts text to upper case
txtPLPreviousNumbers.Text = UCase(txtPLPreviousNumbers.Text)

End Sub
Private Sub txtPLBRNumberSearch_Change()
'Converts text to upper case
txtPLBRNumberSearch.Text = UCase(txtPLBRNumberSearch.Text)

End Sub
Private Sub txtPLCurrentNumberSearch_Change()
'Converts text to upper case
txtPLCurrentNumberSearch.Text = UCase(txtPLCurrentNumberSearch.Text)

End Sub
Private Sub cmdAddNewPLRecord_Click()
'Used to add new records to the Preserved Locomotives Database
Dim lastrow As Long
Dim StockNumber As String
StockNumber = txtPLBRNumber

If Application.WorksheetFunction.CountIf(Sheets("Preserved Locomotives").Range("D4:D1500"), StockNumber) > 0 Then

MsgBox "Stock Number Already Exists", 0, "Duplication Check"

Call UserForm_Initialize
txtPLClass.SetFocus

Exit Sub

End If

lastrow = Sheets("Preserved Locomotives").Range("A" & Rows.Count).End(xlUp).Row
Cells(lastrow + 1, "A").Value = cboPLType
Cells(lastrow + 1, "B").Value = txtPLClass
Cells(lastrow + 1, "C").Value = txtPLSubClass
Cells(lastrow + 1, "D").Value = txtPLBRNumber
Cells(lastrow + 1, "E").Value = txtPLCurrentNumber
Cells(lastrow + 1, "F").Value = txtPLPreviousNumbers
Cells(lastrow + 1, "G").Value = txtPLStockName
Cells(lastrow + 1, "H").Value = DTPicker2
Cells(lastrow + 1, "I").Value = txtPLStockLocation

MsgBox txtPLBRNumber & " has been added to the Preserved Locomotive database", 0, "Stock Number Added"

Call UserForm_Initialize
txtPLClass.SetFocus

End Sub

Private Sub cmdPLBRNumberSearch_Click()
'Used to search for a unique BR stock number if the Preserved Locomotives database and return all corresponding values to the user form

Dim Res As Variant
Dim lastrow
Dim myFind As String

Res = Application.Match(txtPLBRNumberSearch, Sheets("Preserved Locomotives").Range("D4:D1500"), 0)

If IsError(Res) Then
MsgBox "Stock Number Not Found", vbInformation, "Stock Number Not Found"

Call UserForm_Initialize
cboPLType.SetFocus
Exit Sub
End If

lastrow = Sheets("Preserved locomotives").Range("D" & Rows.Count).End(xlUp).Row
myFind = txtPLBRNumberSearch
For Currentrow = 2 To lastrow
If Cells(Currentrow, 4).Text = myFind Then
cboPLType.Value = Cells(Currentrow, 1).Value
txtPLClass.Value = Cells(Currentrow, 2).Value
txtPLSubClass.Value = Cells(Currentrow, 3).Value
txtPLBRNumber.Value = Cells(Currentrow, 4).Value
txtPLCurrentNumber.Value = Cells(Currentrow, 5).Value
txtPLPreviousNumbers.Value = Cells(Currentrow, 6).Value
txtPLStockName.Value = Cells(Currentrow, 7).Value
DTPicker2.Value = Cells(Currentrow, 8).Value
txtPLStockLocation.Value = Cells(Currentrow, 9).Value

Exit For
End If
Next Currentrow
txtPLClass.SetFocus

End Sub

Private Sub cmdPLCurrentNumberSearch_Click()
'Used to search for a unique current stock number if the Preserved Locomotives database and return all corresponding values to the user form

Dim Res As Variant
Dim lastrow
Dim myFind As String

Res = Application.Match(txtPLCurrentNumberSearch, Sheets("Preserved Locomotives").Range("E4:E1500"), 0)

If IsError(Res) Then
MsgBox "Stock Number Not Found", vbInformation, "Stock Number Not Found"

Call UserForm_Initialize
cboPLType.SetFocus
Exit Sub
End If

lastrow = Sheets("Preserved Locomotives").Range("E" & Rows.Count).End(xlUp).Row
myFind = txtPLCurrentNumberSearch
For Currentrow = 2 To lastrow
If Cells(Currentrow, 5).Text = myFind Then
cboPLType.Value = Cells(Currentrow, 1).Value
txtPLClass.Value = Cells(Currentrow, 2).Value
txtPLSubClass.Value = Cells(Currentrow, 3).Value
txtPLBRNumber.Value = Cells(Currentrow, 4).Value
txtPLCurrentNumber.Value = Cells(Currentrow, 5).Value
txtPLPreviousNumbers.Value = Cells(Currentrow, 6).Value
txtPLStockName.Value = Cells(Currentrow, 7).Value
DTPicker2.Value = Cells(Currentrow, 8).Value
txtPLStockLocation.Value = Cells(Currentrow, 9).Value

Exit For
End If
Next Currentrow
txtPLClass.SetFocus

End Sub
Private Sub cmdUpdatePLRecord_Click()
'Used to update existing records

answer = MsgBox("Update the Record?", vbYesNo + vbQuestion, "Update Record?")
If answer = vbNo Then
Call UserForm_Initialize
cboPLType.SetFocus
Else
Cells(Currentrow, 1).Value = cboPLType.Value
Cells(Currentrow, 2).Value = txtPLClass.Value
Cells(Currentrow, 3).Value = txtPLSubClass.Value
Cells(Currentrow, 4).Value = txtPLBRNumber.Value
Cells(Currentrow, 5).Value = txtPLCurrentNumber.Value
Cells(Currentrow, 6).Value = txtPLPreviousNumbers.Value
Cells(Currentrow, 7).Value = txtPLStockName.Value
Cells(Currentrow, 8).Value = DTPicker2.Value
Cells(Currentrow, 9).Value = txtPLStockLocation.Value

MsgBox "Record has been updated", 0, "Record Updated"

Call UserForm_Initialize
txtPLBRNumberSearch.SetFocus

End If
End Sub

Private Sub cmdClearPLForm_Click()
'Clears the User Form

Call UserForm_Initialize

End Sub
Private Sub cmdClosePLForm_Click()
'Closes the User Form

Unload Me

End Sub
 
Upvote 0
A bit difficult to slog through unformatted code (please consider using code tags when you post code).

I am struggling to understand your explanation as I read the code. What is a "record"? I think you mean "data element".

For example, in this code (I reformatted your code and added Code tags):

Rich (BB code):
Private Sub cmdAddNewPLRecord_Click()
   'Used to add new records to the Preserved Locomotives Database
   Dim lastrow As Long
   Dim StockNumber As String
   StockNumber = txtPLBRNumber
  
   If Application.WorksheetFunction.CountIf(Sheets("Preserved Locomotives").Range("D4:D1500"), StockNumber) > 0 Then
     
      MsgBox "Stock Number Already Exists", 0, "Duplication Check"
     
      Call UserForm_Initialize
      txtPLClass.SetFocus
     
      Exit Sub
  
   End If
  
   lastrow = Sheets("Preserved Locomotives").Range("A" & Rows.Count).End(xlUp).row
   Cells(lastrow + 1, "A").Value = cboPLType
' Are you saying that execution stops here until the MsgBox below is shown and closed?
   Cells(lastrow + 1, "B").Value = txtPLClass
   Cells(lastrow + 1, "C").Value = txtPLSubClass
   Cells(lastrow + 1, "D").Value = txtPLBRNumber
   Cells(lastrow + 1, "E").Value = txtPLCurrentNumber
   Cells(lastrow + 1, "F").Value = txtPLPreviousNumbers
   Cells(lastrow + 1, "G").Value = txtPLStockName
   Cells(lastrow + 1, "H").Value = DTPicker2
   Cells(lastrow + 1, "I").Value = txtPLStockLocation
  
   MsgBox txtPLBRNumber & " has been added to the Preserved Locomotive database", 0, "Stock Number Added"
  
   Call UserForm_Initialize
   txtPLClass.SetFocus

End Sub

It is possible that the worksheet update is not being displayed yet. I'm not sure why you care as long at the result is correct when this sub exits.
 
Upvote 0
A bit difficult to slog through unformatted code (please consider using code tags when you post code).

I am struggling to understand your explanation as I read the code. What is a "record"? I think you mean "data element".

For example, in this code (I reformatted your code and added Code tags):

Rich (BB code):
Private Sub cmdAddNewPLRecord_Click()
   'Used to add new records to the Preserved Locomotives Database
   Dim lastrow As Long
   Dim StockNumber As String
   StockNumber = txtPLBRNumber
 
   If Application.WorksheetFunction.CountIf(Sheets("Preserved Locomotives").Range("D4:D1500"), StockNumber) > 0 Then
    
      MsgBox "Stock Number Already Exists", 0, "Duplication Check"
    
      Call UserForm_Initialize
      txtPLClass.SetFocus
    
      Exit Sub
 
   End If
 
   lastrow = Sheets("Preserved Locomotives").Range("A" & Rows.Count).End(xlUp).row
   Cells(lastrow + 1, "A").Value = cboPLType
' Are you saying that execution stops here until the MsgBox below is shown and closed?
   Cells(lastrow + 1, "B").Value = txtPLClass
   Cells(lastrow + 1, "C").Value = txtPLSubClass
   Cells(lastrow + 1, "D").Value = txtPLBRNumber
   Cells(lastrow + 1, "E").Value = txtPLCurrentNumber
   Cells(lastrow + 1, "F").Value = txtPLPreviousNumbers
   Cells(lastrow + 1, "G").Value = txtPLStockName
   Cells(lastrow + 1, "H").Value = DTPicker2
   Cells(lastrow + 1, "I").Value = txtPLStockLocation
 
   MsgBox txtPLBRNumber & " has been added to the Preserved Locomotive database", 0, "Stock Number Added"
 
   Call UserForm_Initialize
   txtPLClass.SetFocus

End Sub

It is possible that the worksheet update is not being displayed yet. I'm not sure why you care as long at the result is correct when this sub exits.
Hi

You are correct that the execution stops where you have indicate until the MsgBox is closed. I agree that it does not impact the uploading of details, however, it is an anomoly that I would like to understand.
 
Upvote 0
The only thing I can think of is that it actually populates the worksheet before the message box is displayed but the display is not updated. I suggest you put a breakpoint at the MsgBox statement and see if the data is updated at that point. Screens are updated when you hit a breakpoint.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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