 |
| Access VBA Discuss using VBA for Access programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 28th, 2008, 03:20 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
BOF locating.
Can anyone sort out this piece of my basic ignorance, or at lease my VBasicA ignorance, please?
My code for moving to the NEXT record works all right.
I copied and adjusted it to move to the PREVIOUS record.
It produces an error if I hit Beginning of file, and will not
back if I have hit end of file.
This code was basically copied directly from ACESS 2000 pg 197
----------
THIS WORKS
Private Sub MoveNext_Click()
If IsNull(TransactionDate) Then
'No More records
MsgBox "There are no more records. "
Else
Me.MoveNext.Caption = "Next record."
'MsgBox "Next record. "
DoCmd.GoToRecord acDataForm, "ReceiptDetail", acNext
End If
End Sub
---------------
THIS BOMBS OUT IF AT BEGINNING OF FILE
-------
MOVE TO PREVIOUS RECORD in "documentid" table
Private Sub MoveBack_Click()
If IsNull(TransactionDate) Then
'No More records
MsgBox "There are no more records. "
Else
Me.MoveBack.Caption = "Previous record."
'MsgBox "Next record. "
'While Not Me.BOF
DoCmd.GoToRecord acDataForm, "ReceiptDetail", acPrevious
'End
I tried putting the DATA TABLE name into the place where I put ME, but that does not work.
What should I do to find end or beginning of file?
I am not using a data set, just the table as opened by Access.
I put this code into my new command buttons on a form generated by Access, not one written by me.
Thanks for any help I may get.
Gee - that reminds me of the Grace we used to say at meals when I was young. "For what we are about to receive may The Lord make me truly thankful. Amen."
|
|

February 28th, 2008, 08:24 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
In a recordset, you would use MoveFirst or MoveLast. Can you use this when navigating a form's recordset?
Normally with an Access table, you would use this sort of structure:
rs.MoveFirst
Do Until rs.EOF
...
rs.MoveNext
Loop
This takes you from the first record to the last, and works the other way.
If your recordset comes from a DBMS other than Access, MoveFirst won't make sense since they are not ordered sequentially as with Access.
Did that help?
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|

February 29th, 2008, 10:18 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks mmcdonal for that immediate response.
I am not making sense yet.
I want to move one record forwards or backwards, not to end of file or BOF
But when moving, it hits the end, or the beginning, and either crashes (at BOF) or stops (at the end).
I want to use the EOF and BOF functions to trap these two situations, so I can change direction, but the way the code is written, Access does not accept the name of the data table, which is DocumentId, so the code does not begin to work, hence in my copy posted here I have turned that faulty line into a comment, while still showing it.
The reason I am using command buttons to move Next or Previous is to enable a user to see and click on those two functions easier than if they used the normal navigation buttons for it.
So I have not yet tried the MoveFirst or Movelast.
Finally, do I need to declare a recordset variable within the function , even though I am just looking at the already opened data table, named Documentid, when the function is called. It works OK if I am not at BOF or EOF and shows me every record as needed.
Now I shall look up that reference you gave. The more I learn now, the better, and the sooner I can make up for lost time.
Bronte
|
|

February 29th, 2008, 10:45 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
What you might add to your code is a conditional like:
On Current Event of Form:
If rs.bof = True Then
Me.PreviousButton.Enabled = False
Else
Me.PreviousButton.Enabled = True
End If
If rs.eof = True Then
Me.NextButton.Enabled = False
Else
Me.NextButton.Enabled = True
End If
This would make your code work in all cases since the button is disabled and the user can't click previous if they are at bof. That is the angle I would pursue.
HTH
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|

March 3rd, 2008, 07:59 PM
|
|
Registered User
|
|
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Again thanks.
I must admit to defeat so far.
I am/was used to xBase coding. I am, and have been using Access for years.
I have only just started to TRY to apply code to my database.
The function I showed works, as stated, to a point.
I need to read much more of my books eg the one I named.
Later I will transfer to '07 versions.
But now I just can not find the right code to get EOF.
I have an open database named eExpenditures, and a table called documentid. That is the table I am referring to in this questioning here, and is the one supplying data on the form.
I just cannot find my way into the right code to allow me to use the EOF and BOF functions.
The file is already opened, but not by my code.
I won't be geting much further with coding if i don't sort this out will I ?!
I would have thought that I used "documentid.EOF", but nothing works.
I hope you will forgive me for asking help here when I am so uninformed. Only read about three of four books on Programing in Access, as well as doing a course a few years ago, which was at a time in my life when I coped badly with everything, so it was not completed (i.e. missed the exam at the end) and not applied then.
I just can't see what I am doing wrong!
|
|

March 4th, 2008, 08:55 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
When you create a form, you have the wizard that allows you to add Next, Previous, Beginning and End. Use the Command Button Wizard, then in the Categories List box, select Record Navigation, and then select Next Record, etc.
When the data is bound to the form, you can also use this place markers: Me.CurrentRecord. This will return an integer. 1 is the first record. So you can also use code like:
If Me.CurrentRecord = 1 Then 'This is the BOF
You can also do a record count in the table (put that in a query) and then check the value in the query and put it in a public variable. Then on the Before Insert event on the form, add a 1 to the value in the public variable. Just check the value everytime the form is opened. So:
If Me.CurrentRecord = pbRecordCount Then 'this is the EOF.
I would just use the nav button wizard, though. This is the kind of code they use:
Screen.PreviousControl.SetFocus
DoCmd.FindNext
This is probably the easiest method.
Did that help?
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|
 |