Custom Record Navigation - ms-access

Hi I have an App with nested sub-forms, the record navigation is confusing and I've started moving the Record Navigation controls to the top of the forms to improve that. I'm stuck on the 'n of n' field, is there a generic property(s)/query that will give me this?

For the first n, use the AbsolutePosition property of the form's RecordsetClone. see MSDN
For the second n, use the RecordCount property.
Update: I prefer Mike's approach using CurrentRecord. I created a form with 2 text boxes: txtCurrentRecord and txtRecordCount. Then this procedure for the form's On Current event causes the text boxes to display the same values as the built-in navigation bar.
Private Sub Form_Current()
Me.txtCurrentRecord = Me.CurrentRecord
If Not Me.NewRecord Then
Me.txtRecordCount = Me.RecordsetClone.RecordCount
Else
Me.txtRecordCount = Me.CurrentRecord
End If
End Sub
Update2: I added a procedure to Form Load to make sure RecordCount is accurate.
Private Sub Form_Load()
Me.RecordsetClone.MoveLast
Me.RecordsetClone.MoveFirst
End Sub

Related

MS Access viewing the code of bottom navigations?

I want to edit the code of the bottom navigation buttons. I don't want to add custom buttons.
I will add two lines of code in it.
DoCmd.GoToRecord , "", acNext
Me.VendorList.Value = Me.VendorNo
If you want to execute code when the record changes, you can use the forms On Current event:
Private Sub Form_Current()
Me.VendorList.Value = Me.VendorNo
End Sub
This also triggers when the form is loaded, which is wanted in your case I guess.

How to refer to a checkbox in access navigation form through vba (Detail_Format) in report

I am trying to hide details section in a report if checkbox in navigation form is not checked.
It's not working somehow, please tell me what am I doing wrong.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Forms!DASHBOARD!NavigationSubform.Form!rptMain.chkWaBox.Value = True Then
Detail.Visible = True
End If
End Sub
Thanks for the help in advance.
Is your details section set to visible in design view? If it is, your code will not hide it. Your code only makes it visible if it's hidden.
When using boolean values like this, I like to make the assignment directly, so it doesn't matter what the state of the object is in design, it always gets handled correctly at runtime:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Detail.Visible = Forms!DASHBOARD!NavigationSubform.Form!rptMain.chkWaBox.Value
End Sub

Change caption of button to field in a table using Access

I am trying to make the caption of a button the content of a field from a table.
Everything I am finding online in forums and MS websites only shows using a macro with the OnClick. I need it to display without every clicking. It also needs to dynamically change every time the field from the table changes.
How can I achieve this? Again, I don't want an event such as OnClick, I want it to simply read as the field from the table reads.
You could simply use the Form's OnCurrent event to set the buttons caption.
On Current:
Private Sub Form_Current()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub
This will make it so ever time the selected record changes the button will update. Also just add the code into the specific field's AfterUpdate event so it changes then as well.
After Update:
Private Sub Textbox1_AfterUpdate()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub

MS Access VBA: get selected portion of text from text box?

I'm having trouble find a way to achieve this. Bascially, I just need a way to get the selected portion of text within a text box. The idea is that a user can double click a word within a larger string to automatically search against another set of data.
If can get the below to work, the selected text will simply call a function that runs my search process. The below does not work and many of my online finds only capture the whole text box. Any ideas?
Private Sub txtproductName_DblClick(Cancel As Integer)
Debug.Print txtproductName.SelText
End Sub
I've used MouseUp instead
Private Sub txtproductName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Len(txtproductName.SelText) > 0 Then
'do my thing
Debug.Print txtproductName.SelText
Else
'do nothing
End If
End Sub
You can use the LostFocus event.
Private Sub txtproductName_LostFocus()
MsgBox Me.txtproductName.SelText
End Sub

How can I change the view of an MS Access subform at runtime in VBA code?

This seems like it would be a simple affair, and I am sure I have done this before, but it has been a while since I have done any UI programming in Access. What I need to do is put a button on a form to toggle between datasheet and form view for a subform.
I have found a defaultview property, but nothing that looks like it would toggle the view of the form after it is already open.
Essentially I need the property I can fill in the following code..
sfEmployeeBatchEntry.Form.??? = acFormDS
I found it on my own. I was missing it because it used the silly and clunky RunCommand syntax instead of a simple property or method on the control or form classes.
It ain't pretty, but for posterity, here is the answer.
'You have to set focus to the subform control or the change view call will'
'fail (UGH!)'
MyForm.mySubFormControl.SetFocus
'Change to datasheet view...'
DoCmd.RunCommand acCmdSubformDatasheet
'Change to Form View...'
DoCmd.RunCommand acCmdSubformFormView
I tried a number of different solutions I found on different sites. Some seemed unnecessarily complicated. I cleaned out some of the clutter and found this fits my needs the best.
Dim intView As Integer
intView = Me.Form.CurrentView
If intView = 1 Then
DoCmd.RunCommand (acCmdSubformDatasheetView)
Else
DoCmd.RunCommand (acCmdSubformFormView)
End If
Exit Sub
To Toggle the View of a subForm between Continuous and Datasheet, use this code:
Private Sub cmdToggleView_Click()
If Me.frmViewDetailedTransactionsSub.Form.CurrentView = 1 Then
Me.frmViewDetailedTransactionsSub.SetFocus
DoCmd.RunCommand acCmdSubformDatasheetView
Exit Sub
End If
If Me.frmViewDetailedTransactionsSub.Form.CurrentView = 2 Then
Me.frmViewDetailedTransactionsSub.SetFocus
DoCmd.RunCommand acCmdSubformFormView
Exit Sub
End If
End Sub