Next / Previous Button Code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I have a database & userform im making but now stuck again.
On my userform i can click a button and the first record is imported from the database into the form.
I would now like to browse my other files hence record 3,4,5 etc with the use of Next / Previous buttons.
Can you advise a code for the Next / Previous files.

Thanks
 
Hi,
With regards the additional requirement as mentioned above could we please incorporate the following into the Job Date text box.
I only know it as =TODAY() so when i hit say the New Customer button todays date will allready be there.
The date im using at present is 09/04/2016

I tried a couple of times with a copy like the code below but kept getting an error so ive decided to wait.
Code:
Private Sub txtJobDate_Change()
txtJobDate = Format(txtJobDate.Value, =TODAY()
End Sub
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Ive noticed that when the button Update Record has been clicked i then see a message box saying Recorded Updated.
This box has a yellow triangle / exclamation mark on it.
This looks more like a warning than a confirmation.
Can this logo be changed for something else ?

Or
Can i use an image ive just knocked up but still have it that when clicked it goes away just like the current message box ?
record-updated.jpg

If it is possible then below is the path to the image.

http://www.theremotedoctor.co.uk/database-logo/record-updated.jpg
 
Upvote 0
I appreciate your help "My answer is this" with your form but this form uses my allready in use text boxes. I did try to look into making your form work for me but header size was an issue. This is what I was looking for & now found it.
Thanks.

Awaiting additional code then form complete.
 
Upvote 0
Hi,
Firstly, make a BACKUP of your workbook.

Starting with your UserForm:

You will need FOUR buttons named as follows:

· NextRecord
· PrevRecord
· UpdateRecord
. NewRecord

Captions for NewRecord & UpdateRecord buttons will be applied by code – do not attempt to change them.

Place ALL following code in your UserForms Code Page ensuring that you first delete any existing code with that has the same name.

Code:
 Dim ws As WorksheetDim r As Long
Const StartRow As Long = 6


Private Sub NewRecord_Click()
    Dim i As Integer
    Dim IsNewCustomer As Boolean
    
    IsNewCustomer = CBool(Me.NewRecord.Caption = "New Customer")
    
    ResetButtons IsNewCustomer
    
    Navigate Direction:=IIf(IsNewCustomer, xlNone, xlPrevious)


    'if new customer, add new customer number & Date
    If IsNewCustomer Then
        Me.txtCustomer.Text = Application.Max(ws.Columns(1)) + 1
        Me.txtJobDate.Text = Format(Date, "dd/mm/yyyy")
    End If
    
End Sub


Private Sub NextRecord_Click()
    Navigate Direction:=xlNext
End Sub


Private Sub PrevRecord_Click()
    Navigate Direction:=xlPrevious
End Sub


Private Sub UpdateRecord_Click()
    Dim i As Integer
    Dim IsNewCustomer As Boolean
    Dim msg As String
    
    IsNewCustomer = CBool(Me.UpdateRecord.Caption = "Add Record")
    
    msg = "Record Updated"
    
    If IsNewCustomer Then
    'New record - check all fields entered
    If Not IsComplete(Form:=Me) Then Exit Sub
        r = StartRow
        msg = "New Customer Added"
        ws.Range("A6").EntireRow.Insert
        ResetButtons IsNewCustomer
    End If
    
    On Error GoTo myerror
    Application.EnableEvents = False
    'Add / Update Record
    For i = 1 To UBound(ControlNames)
        With Me.Controls(ControlNames(i))
            'check if date value
            If IsDate(.Text) Then
                ws.Cells(r, i).Value = DateValue(.Text)
            Else
                ws.Cells(r, i).Value = .Text
            End If
        End With
    Next i
    'tell user what happened
    MsgBox msg, 48, msg
    
myerror:
Application.EnableEvents = True
'something went wrong tell user
If Err > 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub


Sub ResetButtons(ByVal Status As Boolean)
    
    With Me.NewRecord
        .Caption = IIf(Status, "Cancel", "New Customer")
        .BackColor = IIf(Status, &HFF&, &H8000000F)
        .ForeColor = IIf(Status, &HFFFFFF, &H0&)
    End With
    
    Me.UpdateRecord.Caption = IIf(Status, "Add Record", "Update Record")
    Me.NextRecord.Enabled = Status
End Sub


Private Sub UserForm_Initialize()
    Set ws = ThisWorkbook.Worksheets("Database")
    Me.UpdateRecord.Caption = "Update Record"
    Me.NewRecord.Caption = "New Customer"
    'start at first record
    Navigate Direction:=xlFirst
End Sub




Sub Navigate(ByVal Direction As XlSearchDirection)
    Dim i As Integer
    Dim LastRow As Long
    
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    r = IIf(Direction = xlPrevious, r - 1, r + xlNext)
    
    'ensure value of r stays within data range
    If r < StartRow Then r = StartRow
    If r > LastRow Then r = LastRow
    
    'get record
    For i = 1 To UBound(ControlNames)
         Me.Controls(ControlNames(i)).Text = IIf(Direction = xlNone, "", ws.Cells(r, i).Text)
    Next i
    
    'set enabled status of next previous buttons
    Me.NextRecord.Enabled = IIf(Direction = xlNone, False, r < LastRow)
    Me.PrevRecord.Enabled = IIf(Direction = xlNone, False, r > StartRow)


End Sub


Place following code In a STANDARD Module ensuring that you first delete any existing code with that has the same name:

Code:
 Option Base 1

Function ControlNames() As Variant
ControlNames = Array("txtCustomer", "txtRegistrationNumber", "txtBlankUsed", "txtVehicle", _
                    "txtButtons", "txtKeySupplied", "txtTransponderChip", "txtJobAction", _
                    "txtProgrammerCloner", "txtKeyCode", "txtBiting", "txtChassisNumber", _
                    "txtJobDate", "txtVehicleYear", "txtPaid")
End Function




Function IsComplete(ByVal Form As Object) As Boolean
    Dim i As Integer
    For i = 1 To UBound(ControlNames)
        IsComplete = CBool(Len(Form.Controls(ControlNames(i)).Text) > 0)
        If Not IsComplete Then
            MsgBox "Please Complete All Fields", 16, "Entry Required"
            Form.Controls(ControlNames(i)).SetFocus
            Exit Function
        End If
    Next i
End Function

Note Option Base 1 statement – This MUST sit at the TOP of the Module – do not move or delete it.

When you open the form the first record will be displayed with following:

- PrevRecord Button disabled.
- NextRecord Button Enabled
- UpdateRecord Button with Caption “Update Record”
- NewRecord Button with Caption “New Customer”


Pressing the NewRecord Button will:
- Clear all Textboxes
- Change Caption to “Cancel”
- -Change Backcolor to RED
- Change UpdateRecord Caption to “New Record”
- Add Next Customer Number to txtCustomer TextBox
- Add Date to txtJobDate TextBox
- Disable NextRecord & PrevRecord Buttons


When you complete all other Fields, Press Button displaying “Add Record” which will:

- Insert In Database worksheet, EntireRow in Row 6
- Add New Record to Row 6
- Change NewRecord Button Caption to “New Customer”
- Change UpdateRecord Button Caption “Update Record” & BackColor to ButtonFace
- Enable NextRecord Button

If when entering a new record, any fields are left blank, a msgbox will be displayed informing user that all fields must be completed. The entry will be cancelled & focus placed to the empty textbox.

If all ok, you should now have a New Record in your database.

Must confess, although I robbed much of this code from a project I helped another with, it turned out bit more involved than I first anticipated to just add this additional facility.

To answer your other question regarding msgbox symbols – I know of only those symbols provided in VBA – unless another knows of some trick than can be applied, can only suggest that if you want to use than image, you replace the msgbox prompt with a custom UserForm.

Finally, I note that you were getting a great deal of assistance from another here, even though may not be using that solution, its always appreciated when any assistance give is acknowledged.

Dave
 
Upvote 0
Hi,
Ive installed the code as advised above.
When i click the green run triangle i get a message pop up.
Compile error.
Expected end of statement.

When i click OK.
The following code is highlighted in red and its selected the r in that code
Code:
Dim ws As WorksheetDim r As Long
 
Upvote 0
If i replace
Code:
 Dim ws As WorksheetDim r As Long
Const StartRow As Long = 6

With the following from your first reply it runs
Code:
Dim ws As Worksheet
Dim r As Long
Const StartRow As Long = 6

I will just go throught it now and report back soon
 
Upvote 0
That sometimes happens when post code using code tags - I missed it but your second post is correct.
 
Upvote 0
Hi,
Here are my findings.

- PrevRecord Button disabled. FINE
- NextRecord Button Enabled FINE
- UpdateRecord Button with Caption “Update Record” FINE
- NewRecord Button with Caption “New Customer” FINE

- Clear all Textboxes FINE but 1784 appears in the Customers name text box,funny as in cell A1 is my balance figure of £1783.00
- Change Caption to “Cancel” FINE
- -Change Backcolor to RED FINE
- Change UpdateRecord Caption to “New Record” TEXT actually shows Add Record
- Add Next Customer Number to txtCustomer TextBox ??? Add next customers NUMBER ?
- Add Date to txtJobDate TextBox FINE
- Disable NextRecord & PrevRecord Buttons FINE

Message to advise complete all fields FINE
I now click Add Record & this new record is added to my database & i see a message New Customer Added.

I now have to press cancel to return to the form where this first entry is displayed. Should i have to press cancel or should it just change on its own ?

Regarding the customer name txt box i did this as a test.
Changed my balance figure to £2345.67 then selected the new customer button.
Yep in the customer text box is 234667 so its looking in the wrong place for something.
If i enter £10.000 then new customer it then shows 10001
So its adding 1 each time.

Other than this pretty impressive.
 
Upvote 0
As published, code worked ok for me but possibly there are other things going on in your form that I am not aware of & may also, have not fully understood some of the fields actions.

What you will need to do is post copy of your workbook with some sample data to a dropbox & will take a look whats going on.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,240
Members
453,026
Latest member
cknader

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