Code adjustment needed to push data directly to database with userform (VBA)

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
sample workbook at:


This code was produced by @dmt32 somewhere in 2017. At the time, what I wanted to do was to simply get data from a range (on the local file) pushed to the database file and his code did the job greatly.

Now, what I want to achieve is:
1. Instead of pushing the data from the local workbook, I want to use a userform instead.
2. On the userform, once I click the command button, the data from the textboxes should be pushed directly to the database file

My ideas are limited at the moment and I want someone to help me fix it.

Thanks in advance.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This is how I got this working - I am ready to welcome better approaches though (If any). Thanks
Code:
Private Sub CommandButton1_Click()
    Dim DatabasePassword$, TemplateSheet$
    Dim wbDatabase As Workbook, wbTemplate As Workbook
    Dim DatabaseRange As Range, i&, j&
    Dim DatabaseName, msg, Data()
    
    DatabasePassword = ""
    '**********************************************************************************************
    'Database Path / Name
    DatabaseName = Cells(19, 9).Value

    If Len(DatabaseName) = 0 Then
        DatabaseName = BrowseFile
        If DatabaseName = False Then Exit Sub
        Cells(19, 9).Value = DatabaseName
    End If
    
    On Error GoTo myerror
    If Not Dir(DatabaseName, vbDirectory) = vbNullString Then
        Application.ScreenUpdating = False
        'Set wbTemplate = ThisWorkbook
        For j = 1 To 9
            i = i + 1
            ReDim Preserve Data(1 To i)
            With Controls("TextBox" & j)
                If Len(.Text) = 10 And IsDate(.Text) Then
                    Data(i) = DateValue(.Text)
                Else
                    Data(i) = .Value
                End If
            End With
        Next j

        Set wbDatabase = Workbooks.Open(DatabaseName, ReadOnly:=False, Password:=DatabasePassword)
        With wbDatabase.Sheets(1)
            Set DatabaseRange = .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row + 1)
        End With
        
        DatabaseRange.Resize(1, 9).Value = Data
      
        wbDatabase.Close True
        msg = Array("Template Data Saved", "Data Saved")
    Else
        msg = Array(DatabaseName & Chr(10) & "File Not Found", "Error")
    End If
    
myerror:
    Application.ScreenUpdating = True
    
    If Err > 0 Then
        If Not wbDatabase Is Nothing Then wbDatabase.Close False
        MsgBox (Error(Err)), 48, "Error"
    Else
        MsgBox msg(0), 48, msg(1)
    End If
    
    Set wbDatabase = Nothing
    Set wbTemplate = Nothing
    
    Set DataEntryRange = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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