ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Move to Next Record with User Form (https://www.excelbanter.com/excel-programming/315875-move-next-record-user-form.html)

cindyk

Move to Next Record with User Form
 
I have a user form where I need to control record navigation. Upon
activation, the form goes to the first record. I have command buttons on the
form that need to move the user to the "previous" or "next" record and I
can't figure out how to make that work. Please help.

I tried this:

dim firstrow as object
set firstrow=shee1.range("b2").end(xldown)
firstrow.offset(1,0).value=textbox1.text
etc.
then I thought I'd loop through this by adding 1 at the end.

I get an error message "object required"

Thank you.

BrianB

Move to Next Record with User Form
 

You are making the job hard for yourself. You only need to keep track o
the current data row. This principle follows for adding new and deletin
records. Here is some basic code to go into a userform :-

Code
-------------------

'- general declaration
Dim CurrentRow As Long
'----------------------------------
'-initialise when form opened
Private Sub UserForm_Initialize()
CurrentRow = 1
Update_Form
End Sub
'-----------------------------------
'- next record button
Private Sub CommandButton1_Click()
CurrentRow = CurrentRow + 1
Update_Form
End Sub
'---------------------------------
'- previous record button
Private Sub CommandButton2_Click()
If CurrentRow 2 Then ' row 1 has headings
Set CurrentRow = CurrentRow - 1
Update_Form
Else
MsgBox ("Top of table")
End If
End Sub
'--------------------------------
'- common use subroutine
Sub Update_Form()
TextBox1.Value = Worksheets("Data").Cells(CurrentRow, 1).Value
TextBox2.Value = Worksheets("Data").Cells(CurrentRow, 2).Value
End Sub
'----------------------------------

-------------------

--
Brian

-----------------------------------------------------------------------
BrianB's Profile: http://www.excelforum.com/member.php...tinfo&userid=5
View this thread: http://www.excelforum.com/showthread.php?threadid=27560


cindyk

Move to Next Record with User Form
 
Thank you Brian. I'll give this a try.

"BrianB" wrote:


You are making the job hard for yourself. You only need to keep track of
the current data row. This principle follows for adding new and deleting
records. Here is some basic code to go into a userform :-

Code:
--------------------

'- general declaration
Dim CurrentRow As Long
'----------------------------------
'-initialise when form opened
Private Sub UserForm_Initialize()
CurrentRow = 1
Update_Form
End Sub
'-----------------------------------
'- next record button
Private Sub CommandButton1_Click()
CurrentRow = CurrentRow + 1
Update_Form
End Sub
'---------------------------------
'- previous record button
Private Sub CommandButton2_Click()
If CurrentRow 2 Then ' row 1 has headings
Set CurrentRow = CurrentRow - 1
Update_Form
Else
MsgBox ("Top of table")
End If
End Sub
'--------------------------------
'- common use subroutine
Sub Update_Form()
TextBox1.Value = Worksheets("Data").Cells(CurrentRow, 1).Value
TextBox2.Value = Worksheets("Data").Cells(CurrentRow, 2).Value
End Sub
'----------------------------------

--------------------


--
BrianB


------------------------------------------------------------------------
BrianB's Profile: http://www.excelforum.com/member.php...info&userid=55
View this thread: http://www.excelforum.com/showthread...hreadid=275601




All times are GMT +1. The time now is 12:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com