Excel VBA Data Entry Form - Adding Multiple Data Entry Tables

Scott27

New Member
Joined
May 10, 2014
Messages
4
Hi guys i downloaded a data entry example from http://www.contextures.com/exceldataentryupdateform.html#download and im currently trying to convert it to my own requirements im having trouble adding another table I cant seem to edit the range of the module (its probably something simple that ive overlooked) im new to VBA and have trouble trying to disect any of the code if you take a look at my modifications what im trying to do is have all my tables on the input sheet display my records on the partsdata sheet through my form on the input sheet and vice versa for input..

You can download my table here from my dropbox to see whats wrong with it :confused: thanks
 
Last edited:
Hi
welcome to the board.

I have had a look at the workbook you downloaded & modified. What you have is so different to the original that it probably is better to start afresh with the code so you understand that its doing what you want.

I am guessing that your VBA experience is a little limted so have put a new approach together which may make it little easier for you to work with. Unfortunatley, I can't seem to access my dropbox so will post all code here:

To get started Delete ALL EXISTING CODE from following modules:


modData
modViewData

From the the worksheet "Input" Delete all code in the sheets code page.

Add all following code to modData module:

Code:
'**************************************************************
'****************Assign Code Tags To Worksheet Buttons*********
Sub Add()
    AddUpdateRecord xlAdd
End Sub
Sub EditUpdate()
    AddUpdateRecord xlEditBox
End Sub
Sub Clear()
    EventsEnable False
    ClearDataEntry xlOn
    EventsEnable True
End Sub
'*************************************************************
'*************************************************************

Sub AddUpdateRecord(ByVal Action As Integer)
    Dim InputData As Range, Item As Range
    Dim Data(54) As Variant
    Dim RecRow As Long
    Dim i As Integer, a As Integer
    Dim msg As Variant
    Dim msgbx As Integer
    msg = Array("Database Updated", "New Record Add To DataBase")
    On Error GoTo exitsub
    If Action = xlAdd Then
        RecRow = GetRow(xlLastCell) + 1
        If Not wsInput.Range("CheckID") Then
            wsInput.Range("CurrRec").Value = RecRow - 1
            a = 1
        Else
            MsgBox "Record Already Exists On Database", 16, "Record Exists"
            GoTo exitsub
        End If
    Else
        RecRow = GetRow(xlRows) + 1
        a = 0
    End If
    Set InputData = wsInput.Range("OrderEntry")
    For Each Item In InputData
        If Item.Value = "" And UCase(Item.Offset(0, 1).Value) = "X" Then
            MsgBox "Please Complete Required Field", 16, "Data Incomplete"
            Item.Select
            GoTo exitsub
        Else
            Data(i) = Item.Value
            i = i + 1
        End If
    Next
    If Action = xlAdd Then
        msgbx = MsgBox(wsInput.Range("OrderID").Value & Chr(10) & _
                     "Do You Want To Add New Record To Database?", 36, "New Record")
        If msgbx = 7 Then GoTo exitsub
    End If
    EventsEnable False
    With wsDatabase
        .Unprotect
        .Range(.Cells(RecRow, 3), .Cells(RecRow, 56)).Value = Data
        With .Cells(RecRow, "A")
            .Value = Now
            .NumberFormat = "mm/dd/yyyy hh:mm:ss"
        End With
        .Cells(RecRow, "B").Value = Application.UserName
    End With
    MsgBox msg(a), 48, msg(a)
exitsub:
    EventsEnable True
End Sub

Sub ClearDataEntry(Optional ByVal ShowAlert As Integer)
    Dim myCopy As Range
    If ShowAlert = xlOn Then
        msg = MsgBox("Are You Sure You Want To Clear Entry?", 36, "Clear Record")
        If msg = 7 Then Exit Sub
    End If
    If wsInput Is Nothing Then Set wsInput = Worksheets("Input")
    'cells to copy from Input sheet - some contain formulas
    Set myCopy = wsInput.Range("OrderEntry")
    'clear input cells that contain constants
    With wsInput
        On Error Resume Next
        With myCopy.Cells.SpecialCells(xlCellTypeConstants)
            .ClearContents
            Application.Goto .Cells(1)    ', Scroll:=True
        End With
        'no record selected, so clear order ID and record #
        .Range("OrderSel").ClearContents
        .Range("CurrRec").Value = 0
        On Error GoTo myerror
    End With
myerror:
    
End Sub

You will need to reassign the Update New Clear buttons to the code tags shown.

Add all code below to modViewData module:

Code:
'***********************************************************************
'****************Assign Code Tags To Worksheet Navigate Buttons*********
Sub FirstRecord()
    GetRecord xlFirst
End Sub
Sub LastRecord()
    GetRecord xlLastCell
End Sub
Sub NextRecord()
    GetRecord xlNext
End Sub
Sub PreviousRecord()
    GetRecord xlPrevious
End Sub
'**********************************************************************
'**********************************************************************

Sub GetRecord(ByVal Direction As Integer, Optional ByVal SearchRow As Long)
    Dim GetData As Variant
    Dim i As Integer
    Dim r As Long, c As Long
    Dim lastRow As Long
    Dim lRec As Long, RecRow As Long
    On Error GoTo myerror
    EventsEnable False
    lastRow = GetRow(xlLastCell)
    With wsInput
        .Unprotect
        lRec = .Range("CurrRec").Value
        Select Case Direction
        Case xlPrevious
            lRec = lRec - 1
        Case xlNext
            lRec = lRec + 1
        Case xlFirst
            lRec = 1
        Case xlLastCell
            lRec = lastRow - 1
        Case xlAll
            lRec = SearchRow - 1
        End Select
        If lRec < 1 Then lRec = 1
        If lRec > lastRow - 1 Then lRec = lastRow - 1
        .Range("CurrRec").Value = lRec
        RecRow = lRec + 1
        GetData = Application.Transpose(wsDatabase.Range(wsDatabase.Cells(RecRow, 3), wsDatabase.Cells(RecRow, 56)).Value)
        For i = LBound(GetData) To UBound(GetData)
            Select Case i
            Case 1    'to 16
                c = 4
                r = 7
            Case 17    'to 22
                c = 4
                r = 24
            Case 23    'to 28
                c = 11
                r = 7
            Case 29    'to 34
                c = 11
                r = 14
            Case 35    'to 40
                c = 11
                r = 21
            Case 41    'to 53
                c = 17
                r = 7
            Case 54
                r = 31
                c = 4
            End Select
            wsInput.Cells(r, c).Value = GetData(i, 1)
            r = r + 1
        Next i
        wsInput.Range("OrderSel").Value = .Range("OrderID").Value
    End With
myerror:
    EventsEnable True
End Sub

You will need to reassign each of the Navigate buttons to the code tags shown.


Add a new module & name it modFunction
add following code:

Code:
Public wsDatabase As Worksheet
Public wsInput As Worksheet
Sub EventsEnable(ByVal State As Boolean)
    With Application
        .EnableEvents = State
        .ScreenUpdating = State
        .Calculation = IIf(State, xlCalculationAutomatic, xlCalculationManual)
    End With
End Sub

Public Function GetRow(ByVal WhichRow As Integer) As Long
    If wsInput Is Nothing Then Set wsInput = Worksheets("Input")
    If wsDatabase Is Nothing Then Set wsDatabase = Worksheets("PartsData")
    If WhichRow = xlLastCell Then
        With wsDatabase
            GetRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        End With
    Else
        GetRow = wsInput.Range("CurrRec").Value
    End If
End Function

Place the following code in the Input worksheets code page:


Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim FoundCell As Range
    Dim Search As String
    Dim temp As String
    Dim msg As Integer
    If wsInput Is Nothing Then Set wsInput = Worksheets("Input")
    If wsDatabase Is Nothing Then Set wsDatabase = Worksheets("PartsData")
    Search = ""
    EventsEnable False
    Select Case Target.Address
    Case Me.Range("OrderSel").Address
        Search = Me.Range("OrderSel").Value
    Case Me.Range("OrderID").Address
        If Me.Range("CheckID") = True Then
            With Me.Range("OrderSel")
                .Value = Me.Range("OrderID").Value
                Search = .Value
            End With
        Else
            'record not found
            Me.Range("OrderSel").ClearContents
            Me.Range("CurrRec").Value = 0
            msg = MsgBox("Do You Want To Clear All Fields?", 36, "Add New Record")
            If msg = 6 Then
                temp = Me.Range("OrderID").Value
                ClearDataEntry xlOff
                Me.Range("OrderID").Value = temp
                Me.Range("D8").Select
            End If
        End If
    Case Else
        GoTo exitHandler
    End Select
    If Len(Search) > 0 Then
        Set FoundCell = wsDatabase.Columns(3).Find(Search)
        If Not FoundCell Is Nothing Then
            GetRecord xlAll, FoundCell.Row
        Else
            'do nothing
        End If
    End If
exitHandler:
    EventsEnable True
    Exit Sub
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = Me.Range("OrderSel").Address Then
        With Me.Range("OrderSel").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                 xlBetween, Formula1:="=OrderIDList"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
    End If
End Sub


Code will need testing by you & updated to meet your need but hopefully, will get you going in the right direction.


good luck
Dave
 
Last edited:
Upvote 0
OMG Thank you so much everything works you are a genius :biggrin: Can I ask you a question? Is it possible to store the "PartsData" sheet in the cloud and have multiple users able to work from the input sheet at the same time?

I would also like to somehow automatically asign ID reference numbers when creating a new record starting at something like 10910 then if someone clicks new again it will prefill 10911 the current record is just dummy data.

Heres the updated version dropbox link, Thanks again dmt32
 
Last edited by a moderator:
Upvote 0
OMG Thank you so much everything works you are a genius :biggrin: Can I ask you a question? Is it possible to store the "PartsData" sheet in the cloud and have multiple users able to work from the input sheet at the same time?

I would also like to somehow automatically asign ID reference numbers when creating a new record starting at something like 10910 then if someone clicks new again it will prefill 10911 the current record is just dummy data.

Thanks again dmt32

Glad worked ok for you.

To answer your questions - you can use excel as a flat file database & have template workbooks read / write data to & from it but coding is extensive & end product would be very limited. For such an application I would suggest that you look at something like Access.

You can add auto numbering & will have a look when I have a moment.

Dave
 
Upvote 0
Glad worked ok for you.

To answer your questions - you can use excel as a flat file database & have template workbooks read / write data to & from it but coding is extensive & end product would be very limited. For such an application I would suggest that you look at something like Access.

You can add auto numbering & will have a look when I have a moment.

Dave

Also when i try to apply data validation to certain fields on the input sheet it doesn't save it when i reopen it any ideas why? im not to fussed about having the data in the cloud just the autonumbering.
 
Upvote 0
Hi try these updates:

Replace Worksheet_Change event in Input Sheets code page with following:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'V2 12 - 5 - 2014
    Dim FoundCell As Range
    Dim Search As String
    Dim temp As String
    Dim msg As Integer    

    If wsInput Is Nothing Then Set wsInput = Worksheets("Input")
    If wsDatabase Is Nothing Then Set wsDatabase = Worksheets("PartsData")
    Search = ""
    EventsEnable False
    Select Case Target.Address
    Case Me.Range("OrderSel").Address
        Search = Me.Range("OrderSel").Value
    Case Me.Range("OrderID").Address
        If Me.Range("CheckID") = True Then
            With Me.Range("OrderSel")
                .Value = Me.Range("OrderID").Value
                Search = .Value
            End With
        Else
            If Target.Value = "" Or wsInput.Range("CurrRec").Value = 0 Then GoTo exitHandler
            'record not found
            MsgBox "Record Not Found", 48, "Not Found"
            Application.Undo
        End If
    Case Else
        GoTo exitHandler
    End Select
    If Len(Search) > 0 Then
        Set FoundCell = wsDatabase.Columns(3).Find(Search)
        If Not FoundCell Is Nothing Then GetRecord xlAll, FoundCell.Row
    End If
exitHandler:
    EventsEnable True
End Sub

Delete ALL Code in modData module and replace with following:

Code:
'**************************************************************
'****************Assign Code Tags To Worksheet Buttons*********
Sub Add()
    AddUpdateRecord xlAdd
End Sub
Sub EditUpdate()
    AddUpdateRecord xlEditBox
End Sub
Sub Clear()
    EventsEnable False
    ClearDataEntry
    EventsEnable True
End Sub
'*************************************************************
'*************************************************************

Sub AddUpdateRecord(ByVal Action As Integer)
'V2 12 - 5 - 2014
    Dim InputData As Range, Item As Range
    Dim Data(54) As Variant
    Dim RecRow As Long
    Dim i As Integer, a As Integer
    Dim msg As Variant
    Dim msgbx As Integer
    msg = Array("Database Updated", "New Record Add To DataBase")
    On Error GoTo exitsub
    If Action = xlAdd Then
        RecRow = GetRow(xlLastCell) + 1
        If Not wsInput.Range("CheckID") Then
            wsInput.Range("CurrRec").Value = RecRow - 1
            a = 1
        Else
            MsgBox "Record Already Exists On Database", 16, "Record Exists"
            GoTo exitsub
        End If
    Else
        RecRow = GetRow(xlRows) + 1
        a = 0
    End If
    Set InputData = wsInput.Range("OrderEntry")
    For Each Item In InputData
        If Item.Value = "" And UCase(Item.Offset(0, 1).Value) = "X" Then
            MsgBox "Please Complete Required Field", 16, "Data Incomplete"
            Item.Select
            GoTo exitsub
        Else
            Data(i) = Item.Value
            i = i + 1
        End If
    Next
    If Action = xlAdd Then
        msgbx = MsgBox(wsInput.Range("OrderID").Value & Chr(10) & _
                       "Do You Want To Add New Record To Database?", 36, "New Record")
        If msgbx = 7 Then GoTo exitsub
    End If
    EventsEnable False
    With wsInput
        .Range("OrderID").Value = .Range("Z5").Value
        .Range("Z5").Value = .Range("Z5").Value + 1
        .Buttons(8).Caption = "Clear"
    End With

    With wsDatabase
        .Unprotect
        .Range(.Cells(RecRow, 3), .Cells(RecRow, 56)).Value = Data
        With .Cells(RecRow, "A")
            .Value = Now
            .NumberFormat = "mm/dd/yyyy hh:mm:ss"
        End With
        .Cells(RecRow, "B").Value = Application.UserName
    End With
    MsgBox msg(a), 48, msg(a)
exitsub:
    EventsEnable True
End Sub
Sub ClearDataEntry()
'V2 12 - 5 - 2014
    Dim myCopy As Range
    If wsInput Is Nothing Then Set wsInput = Worksheets("Input")
    With wsInput
        If .Buttons(8).Caption = "Clear" Then
            If Not NewRefID Then GoTo myerror
            msg = MsgBox("Do You Want To Clear Entry To Add New Record?" & Chr(10) & _
                         "            Press Yes To Continue", 36, "Add New Record")
            If msg = 7 Then Exit Sub
            'cells to copy from Input sheet - some contain formulas
            Set myCopy = .Range("OrderEntry")
            'clear input cells that contain constants
            On Error Resume Next
            With myCopy.Cells.SpecialCells(xlCellTypeConstants)
                .ClearContents
                Application.Goto .Cells(1)    ', Scroll:=True
            End With
            'no record selected, so clear order ID #
            .Range("OrderSel").ClearContents
            'store active record row
            .Range("Z6").Value = .Range("CurrRec").Value
            'clear record
            .Range("CurrRec").Value = 0
            On Error GoTo myerror
            'change button cption
            .Buttons(8).Caption = "Cancel"
            'set new order ID
            .Range("OrderID").Value = .Range("Z5").Value
            .Range("D8").Select
        Else
            'return to active record
            .Buttons(8).Caption = "Clear"
            'restore active record
            GetRecord xlAll, .Range("Z6").Value + 1
        End If
    End With
myerror:
End Sub

    Function NewRefID() As Boolean
'V2 12 - 5 - 2014
    Dim Response As Variant
    With wsInput
        .Unprotect
        .Columns("X:Z").Hidden = True
        With .Range("Z5")
            If IsEmpty(.Value) Or Not IsNumeric(.Value) Then
Retry:
                Response = InputBox("Please Enter Reference ID Start No.", "Enter ID")
                If StrPtr(Response) = 0 Then
                    Exit Function
                ElseIf Len(Response) = 0 Then
                    MsgBox "Reference ID Cannot be Blank", 16, "Entry Required"
                    GoTo Retry
                Else
                    If IsNumeric(Response) Then
                        .NumberFormat = "00000"
                        .Value = CLng(Response)
                    Else
                        MsgBox "Invalid Input" & Chr(10) & _
                               "Numeric Input Only", 16, "Error"
                        GoTo Retry
                    End If
                End If
            End If
        End With
    End With
    NewRefID = True
End Function

Code uses Clear button to add a new Ref ID when form is cleared. If new number does not exist, you will be prompted to input a start number. After this, numbers are auto incremented. Note: Input is numeric only.

Clear Button Caption changes to "Cancel" - if you decide to abort the new record you will be returned to the last active record.

Don't know why you are having problem you describe - when added & tested updated code, place copy in your dropbox & will have a look when can.

Dave
 
Last edited:
Upvote 0
Thanks Dave its working dont understand why the data validation wont save either could it be because its not saved as a macro enabled workbook im using office 2013 dropbox link.
 
Last edited by a moderator:
Upvote 0
Which fields are meant to have data validation? - apart from Ref ID field, none seem to be showing.

If you are saving an xls workbook with macros to a later version - you will need to save it as macro enabled (xlsm)

otherwise macro functionality will be lost.

Dave
 
Upvote 0
Hi,
Had a look at your link and as another from the board (DocAELstein) has pointed out to you, you are trying to develop a major project which although probably within the scope of my abilities, I do not have time to spend on - what free time I have, I try to assist as many on board as possible with smaller issues that can be quickly resolved. You may want to consider approaching a free lance MS office programmer or specialist organisation for help but of course, this will come at a cost.

Dave
 
Upvote 0

Forum statistics

Threads
1,226,848
Messages
6,193,315
Members
453,790
Latest member
yassinosnoo1

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