VBA to write a record to another Excel workbook (used as a central database) from command button on a userform

nhumensky

New Member
Joined
Apr 12, 2016
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a workbook that launches a userform upon opening. It is essentially a scorecard for supervisors to use for taking notes on the employees on their team. When selecting an employee's name from the combobox, it populates a bunch of labels with metrics for the employee. At the bottom I have a ListView control that is filled with notes that have been entered on the agent in the past.

Originally, I had a textbox above the ListView where you could type out a note and click a command button to enter a "record" on another worksheet containing the employee's name, the note, and a timestamp of when the button was clicked. However, issues came up when a supervisor pointed out that a note they entered did not show up on the scorecard ListView for another supervisor with the same file/workbook/scorecard open.

Is there anyway to use a separate Excel workbook on a network drive as a central database? So when one supervisor enters a note, it writes it to that workbook, saves the workbook, and then it can show up on another copy of the scorecard? I tried creating a Shared workbook, since it may be feasible that two supervisors could enter a note at the same time and would need the workbook eligible to be edited by multiple users. However, this did not work.

Here is the code for when you press the command button to add the note. The xlw variable is the file path on the network drive for where I have the dbNotes workbook "database", which is Shared.

Code:
Private Sub cmdAddNote_Click()


    Dim lastrow As Integer
    Dim lastrow2 As Integer
    Dim xl0 As New Excel.Application
    Dim xlw As New Excel.Workbook
    Set xlw = xl0.Workbooks.Open("\\prod\test\Scorecard\Notes\dbNotes.xlsx")
    
    'Helps speed up the processing
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .DisplayStatusBar = False
    End With
    
    'Removes the current agent's notes from the ListView control.
    With lsvNotes
        .ColumnHeaders.Clear
        .ListItems.Clear
    End With
    
    'Error handling for if supervisor tries to add a note without
    'typing anything in the notes textbox and has no agent selected.
    
    If Me.txtNotes.Value = "" And Me.cboChooseAgent.Value = "" Then
        MsgBox "You must select an agent " & vbNewLine & "and add a note."
    ElseIf Me.cboChooseAgent.Value = "" Then
        MsgBox "You must select an agent."
    ElseIf Me.txtNotes.Value = "" Then
        MsgBox "There is no note to be added."
    Else
    
        'sets lastrow2 to be the end of the recordset on worksheet(database)
        'puts the agent's name in the first column of the record
        'the note in the second column of the record
        'and the timestamp of the button press in the third column
        'it quickly saves and closes the dbNotes workbook
        'and clears the variables
        
        lastrow2 = xl0.Sheets("Notes").Range("A1").CurrentRegion.Rows.count + 1
        xl0.Sheets("Notes").Cells(lastrow2, 1) = Me.cboChooseAgent.Value
        xl0.Sheets("Notes").Cells(lastrow2, 2) = Me.txtNotes.Value
        xl0.Sheets("Notes").Cells(lastrow2, 3) = Format(Now, "mm/dd/yy hh:mm")
        xlw.Save
        xlw.Close
        
        Set xl0 = Nothing
        Set xlw = Nothing
        
    End If
    
    'blanks out the notes textbox
    Me.txtNotes.Value = ""
    
    'refills the ListView with all of the current notes for the agent.
    Call filllsvNotes
    
    'turns all of these back on.
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
        .DisplayStatusBar = True
    End With
    
End Sub


Any ideas would be greatly appreciated.

Thank you
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi nhumensky,

One possibility could be to have the code create a new workbook for each entry, save it in a specified network folder, then you could add some code to the master workbook so that when it opens it will go into that folder and gather up all the entry workbooks, add them to the master and then delete the entry files...

Not sure if that's a good solution but just a thought...

Hope this helps,
Cheers,
Alan.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,665
Members
449,114
Latest member
aides

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