By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,257 Members | 2,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,257 IT Pros & Developers. It's quick & easy.

Disable navigation button

didajosh
P: 47
I have made a form and added navigation buttons using the wizard.
When the focus is on the first Record, I want the "Previous Record Button" to disable.
So in the code in Click event I want to write something like:
Expand|Select|Wrap|Line Numbers
  1. If Current Record = BOF
  2. then Command82.Enabled = False 
  3. End if
How do I write this...?
Apr 19 '10 #1
Share this Question
Share on Google+
11 Replies


patjones
Expert 100+
P: 931
Hi -

If this is a bound form, you could try putting this in the On Current event for the form:

Expand|Select|Wrap|Line Numbers
  1. Dim rst As Recordset
  2. Set rst = Me.RecordsetClone
  3.  
  4. If rst.BOF Then
  5.      cmdPreviousRecord.Enabled = False
  6. Else
  7.      cmdPreviousRecord.Enable = True
  8. End If

A shorthand way of writing the If construct is:

Expand|Select|Wrap|Line Numbers
  1. cmdPreviousRecord.Enabled = Not rst.BOF

Pat
Apr 19 '10 #2

NeoPa
Expert Mod 15k+
P: 31,494
I have had problems with this logic before, which I must admit I never really got to the heart of. I forget under which circumstances I found .BOF & .EOF to be unreliable, but they were what I tried first.
Expand|Select|Wrap|Line Numbers
  1. With Me
  2.     'Bug where .AbsolutePosition can be 0 even when on NewRecord
  3.     .cmdPreviousRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or _
  4.                                  (.NewRecord))
  5. ...
  6. End With
Apr 20 '10 #3

patjones
Expert 100+
P: 931
I have successfully used .BOF and .EOF in various contexts before; however in looking at what I wrote yesterday I don't think putting the code

Expand|Select|Wrap|Line Numbers
  1. Dim rst As Recordset
  2. Set rst = Me.RecordsetClone

in with the rst.BOF test will work because the assignment of rst will, I think, automatically place the bookmark on the first record; so rst.BOF would just always evaluate to true.

I'm going to make a little mock-up form and play with this, pending completion of some matters here at work.

Pat
Apr 20 '10 #4

didajosh
P: 47
Hi,
Thank you for your time and suggestions.
I tried to implement what zepphead80 told me.

I don't think rst.BOF would just always evaluate to true, on the contrary its always evaluating as False. I put a couple of msg boxed to check if the condition is being satisfied or not (following code) but turns out "True" msgbox never appears.
Expand|Select|Wrap|Line Numbers
  1.    Dim rst As Recordset
  2.    Set rst = Me.RecordsetClone
  3.    If rst.BOF Then
  4.    MsgBox "True"
  5.    CmdPrevButton.Enabled = False
  6.    Else
  7.    MsgBox "False"
  8.    CmdPrevButton.Enabled = True
  9.    End If
The button remains enabled.

Dipali
Apr 20 '10 #5

patjones
Expert 100+
P: 931
Dipali -

The button will in fact turn gray.

As I mentioned in my post this morning, I didn't think this code would work properly. I have a mock database set up to test out how to do this and I'll let you know what I come up with, shortly.

Pat
Apr 20 '10 #6

didajosh
P: 47
Hi,
I guess I found a solution.
Expand|Select|Wrap|Line Numbers
  1.  Dim rst As Recordset
  2.    Set rst = Me.Recordset
  3.    If rst.AbsolutePosition = 0 Then
  4.    Command28.Enabled = False
  5.    Else
  6.    Command28.Enabled = True
  7.    End If
It works perfectly. I just stumbled on "AbsolutePosition".
I am not sure if its alright to use it, or if this is the right way.

-Dipali
Apr 20 '10 #7

patjones
Expert 100+
P: 931
Hi Dipali -

That is in fact what I basically did as well. Upon researching BOF I remembered an important fact, which is that it doesn't get set to false until you are actually on the first record AND try to move to a previous record (same thing for EOF, at the last record).

What I ended up doing was declaring a recordset visible to the entire form module, "rst". I then coded the Form Open event, and Click events for the two command buttons as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNext_Click()
  2.  
  3. Me.cmdPrevious.Enabled = True
  4.  
  5. If rst.AbsolutePosition < rst.RecordCount - 1 Then
  6.  
  7.     rst.MoveNext
  8.     Me.Bookmark = rst.Bookmark
  9.  
  10.     If rst.AbsolutePosition = rst.RecordCount - 1 Then
  11.         Me.cmdPrevious.SetFocus
  12.         Me.cmdNext.Enabled = False
  13.     End If
  14.  
  15. End If
  16.  
  17. End Sub
  18.  
  19. Private Sub cmdPrevious_Click()
  20.  
  21. Me.cmdNext.Enabled = True
  22.  
  23. If rst.AbsolutePosition > 0 Then
  24.  
  25.     rst.MovePrevious
  26.     Me.Bookmark = rst.Bookmark
  27.  
  28.     If rst.AbsolutePosition = 0 Then
  29.         Me.cmdNext.SetFocus
  30.         Me.cmdPrevious.Enabled = False
  31.     End If
  32.  
  33. End If
  34.  
  35. End Sub
  36.  
  37. Private Sub Form_Open(Cancel As Integer)
  38.  
  39. Set rst = Me.RecordsetClone
  40. rst.MoveLast
  41. rst.MoveFirst
  42. Me.Bookmark = rst.Bookmark
  43. Me.cmdPrevious.Enabled = False
  44.  
  45. End Sub
  46.  

Let me know what you think.

Pat
Apr 20 '10 #8

didajosh
P: 47
Hi Pat,
Thank you for your generous help. I really appreciate it.
I need to try this and will get back with you.

Dipali
Apr 20 '10 #9

NeoPa
Expert Mod 15k+
P: 31,494
Hi Dipali.

From your comments I assume you didn't notice my post #3 from earlier. .AbsolutePosition is good, but as you'll see from that post, it's not perfect on its own. You would also need to test .NewRecord if you want the code to work logically.
Apr 21 '10 #10

didajosh
P: 47
Hi,
Thank you so much for the help.I am very grateful to you guys.
I clubbed the two solutions suggested to me.
It works perfectly.
Its in the Current event of the form. And its for previous, next, first and last buttons.
Expand|Select|Wrap|Line Numbers
  1. With Me
  2.   CmdPreviousRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
  3.   CmdNextRecord.Enabled = ((.Recordset.AbsolutePosition <> .Recordset.RecordCount - 1) Or (.NewRecord))
  4.   CmdFirstRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
  5.   CmdLastRecord.Enabled = ((.Recordset.AbsolutePosition <> .Recordset.RecordCount - 1) Or (.NewRecord))
  6. End With
Apr 21 '10 #11

NeoPa
Expert Mod 15k+
P: 31,494
That's very good thinking. You have clearly grasped the point and built on it.

I would consider slight changes though, to support the logic :
Expand|Select|Wrap|Line Numbers
  1. With Me.
  2.   .CmdPreviousRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
  3.   .CmdNextRecord.Enabled = (Not .NewRecord)
  4.   .CmdFirstRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
  5.   .CmdLastRecord.Enabled = ((.Recordset.RecordCount > 0) And (.Recordset.AbsolutePosition <> .Recordset.RecordCount - 1))
  6. End With
Apr 21 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.