Hi,
I have two workbooks, WB1 & WB2 as an example. WB1 contains my UserForm and WB2 contains the master data where the entered information from the UserForm is placed.
My issue is that multiple people need to submit data via the UserForm yet if the 'Submit' button is pressed by two or more people at the same time then obviously only one can access WB2 and the other receives an error to the effect that 'a workbook with that name already exists'. I am looking for a way for my code on the 'Submit' button to check if WB2 is already open prior to continuing and if it is, then to pause the process and offer a 'Retry' button which would then continue with the 'Submit' process. I have made WB1 read only so that can now be opened by as many people as necessary but I can't fathom what to do with the check of WB2.
My code for the 'Submit' function is below however I am very new to this and it has been gleamed from reading various forum posts so please feel free to suggest other options. I haven't completely lost the plot but I have had to remove a few bits of the code as they contain identifiable info so I have replaced this with XXXX hence there are a few blocks of these throughout the code.
Thanks in advance.
I have two workbooks, WB1 & WB2 as an example. WB1 contains my UserForm and WB2 contains the master data where the entered information from the UserForm is placed.
My issue is that multiple people need to submit data via the UserForm yet if the 'Submit' button is pressed by two or more people at the same time then obviously only one can access WB2 and the other receives an error to the effect that 'a workbook with that name already exists'. I am looking for a way for my code on the 'Submit' button to check if WB2 is already open prior to continuing and if it is, then to pause the process and offer a 'Retry' button which would then continue with the 'Submit' process. I have made WB1 read only so that can now be opened by as many people as necessary but I can't fathom what to do with the check of WB2.
My code for the 'Submit' function is below however I am very new to this and it has been gleamed from reading various forum posts so please feel free to suggest other options. I haven't completely lost the plot but I have had to remove a few bits of the code as they contain identifiable info so I have replaced this with XXXX hence there are a few blocks of these throughout the code.
Thanks in advance.
Code:
Private Sub cmdTest_Click() 'Test command button
Cancel = 0
[COLOR=#008000]'Checks for empty fields in UserForm (except Remarks)[/COLOR]
If [COLOR=#0000ff][B]XXXX[/B][/COLOR] = "" Then
Cancel = 1
[B]XXXX[/B].SetFocus
ElseIf DDTxtDate.Text = "" Then
Cancel = 1
DDTxtDate.SetFocus
ElseIf DDcboTime1.Text = "" Then
Cancel = 1
DDcboTime1.SetFocus
ElseIf DDcboTime2.Text = "" Then
Cancel = 1
DDcboTime2.SetFocus
ElseIf DDcbo[B]XXXX[/B].Text = "" Then
Cancel = 1
DDcbo[B]XXXX[/B].SetFocus
ElseIf DDcbo[B]XXXX[/B].Text = "" Then
Cancel = 1
DDcbo[B]XXXX[/B].SetFocus
ElseIf DDTxtAge.Text = "" Then
Cancel = 1
DDTxtAge.SetFocus
ElseIf DDcboGender = "" Then
Cancel = 1
DDcboGender.SetFocus
ElseIf DDcboActions = "" Then
Cancel = 1
DDcboActions.SetFocus
End If
If Cancel = 1 Then
MsgBox "Not All Values Have Been Entered", vbCritical, "[B]XXXX[/B] "
Exit Sub
End If
[COLOR=#008000]'Move data from UserForm to worksheet[/COLOR]
Dim rw As Long 'Gets next available row
Dim wbk As Workbook
Set wbk = Workbooks.Open("\\[B]XXXX[/B].xlsx", Password:="[B]XXXX[/B] ")
With wbk.Sheets("[B]XXXX[/B] Returns") 'Selects sheet
rw = .Range("A" & Rows.Count).End(xlUp).Row + 1 'Gets next available row in sheet
[COLOR=#008000]'Put the UserForm entries in the found blank row[/COLOR]
.Range("A" & rw).Value = DD[B]XXXX[/B].Value
.Range("B" & rw).Value = DD[B]XXXX[/B].Value
.Range("C" & rw).Value = DD[B]XXXX[/B].Value
.Range("D" & rw).Value = DDTxtDate.Value
.Range("E" & rw).Value = DDcboTime1.Value & ":" & DDcboTime2.Value [COLOR=#008000]'Concatenates the two time entry fields[/COLOR]
.Range("F" & rw).Value = DDTxtAge.Value
.Range("G" & rw).Value = DDcboGender.Value
.Range("H" & rw).Value = DDcbo[B]XXXX[/B].Value
.Range("I" & rw).Value = DDcbo[B]XXXX[/B].Value
.Range("J" & rw).Value = DDcboActions.Value
.Range("K" & rw).Value = DDTxtRemarks.Value
.Range("L" & rw).Value = Date + Time 'Adds a date/time stamp for the entry
[COLOR=#008000]'Puts border round selection[/COLOR]
.Range("A" & rw).Borders.LineStyle = xlContinuous
.Range("B" & rw).Borders.LineStyle = xlContinuous
.Range("C" & rw).Borders.LineStyle = xlContinuous
.Range("D" & rw).Borders.LineStyle = xlContinuous
.Range("E" & rw).Borders.LineStyle = xlContinuous
.Range("F" & rw).Borders.LineStyle = xlContinuous
.Range("G" & rw).Borders.LineStyle = xlContinuous
.Range("H" & rw).Borders.LineStyle = xlContinuous
.Range("I" & rw).Borders.LineStyle = xlContinuous
.Range("J" & rw).Borders.LineStyle = xlContinuous
.Range("K" & rw).Borders.LineStyle = xlContinuous
.Range("L" & rw).Borders.LineStyle = xlContinuous
[COLOR=#008000]'Closes Master Data workbook[/COLOR]
wbk.Save
wbk.Close
[COLOR=#008000]'Clear UserForm entries[/COLOR]
DD[B]XXXX[/B].Value = ""
DD[B]XXXX[/B].Value = ""
DD[B]XXXX[/B].Value = ""
DDTxtDate.Value = ""
DDcboTime1.Value = ""
DDcboTime2.Value = ""
DDcbo[B]XXXX[/B].Value = ""
DDcbo[B]XXXX[/B].Value = ""
DDTxtAge.Value = ""
DDcboGender.Value = ""
DDcboActions.Value = ""
DDTxtRemarks.Value = ""
MsgBox "Your entry has been submitted.", , "[B]XXXX[/B]"
ThisWorkbook.Saved = True [COLOR=#008000]'Stops the "Do you want to save the changes" box[/COLOR]
Application.Quit
End With
End Sub