Navigate programmatically through the records of a continuous form - forms

I would like to navigate through the records of a continuous form in Access 97. I don't find how to do it. This is what I tried:
Me.RecordSetClone.MoveFirst moves to the first record logically, but not in the UI. Also the CurrentRecord property does not change.
I cannot set the CurrentRecord property, it is readonly. Me.CurrentRecord = 1 gives an error.
DoCmd.GoToRecord Record:=acFirst seems to have no effect.
What is the correct way to move to the first record in a continuous form (and to the next/previous)?

Use the Bookmark property of RecordsetClone and Form.
Caveat: I'm pretty sure all this worked in Access 97, but that was a really long time ago.
Sub DemoNavigate()
Dim RS As DAO.Recordset
Set RS = Me.RecordsetClone
RS.MoveFirst
' or
RS.AbsolutePosition = 0
' Navigate in form
Me.Bookmark = RS.Bookmark
' next record
RS.MoveNext
' or
RS.AbsolutePosition = 1
Me.Bookmark = RS.Bookmark
' Move to searched record
RS.FindFirst "someField = 42"
Me.Bookmark = RS.Bookmark
End Sub

Related

insert and delete text after an Range-position in Word

I have a SET-field in Word 2007. After the set-field there could be everything (text,bookmark, SET field,...). I want to add a text (e.g. "exampletext") in between.
After this I want to delete this inserted text (but I don't want to search through the whole document).
Is there a method?
Trial 1 (it inserts it in the field - and not after the field):
' xStartReturn is a field
Dim myExampletext As WordApp.Range = objDoc.Range(xStartReturn.Code.End, xStartReturn.Code.End )
myExampletext.Text = "exampletext"
Trial 2 (leads to the problem that I don't get the Range-field to delete the exampletext afterwards):
xEndeReturn.insertAfter("exampletext")
Trial 3:
'xStartReturn.Code.End + 1 doesn't work.. but I found out that the "}"-Sign in the setField is +20 after xStartReturn.Code.End. Theoretical this should work - but there could be e.g. also paragraph afterwards.
'-> I can automatically check that there is a paragraph - but why is the exampletext added **after** the paragraph?
Dim example As WordApp.Range = objDoc.Range(xStartReturn.Code.End + 20, xStartReturn.Code.End + 20)
example.Text = "exampletext"
Dim later As WordApp.Range = objBasisvorlage_.Range(objXStartReturn.Code.End + 20, objXStartReturn.Code.End + 20 + "SDFSD".Length) 'this is wrong?!
later.Delete()
The following works for me. Since you didn't give us a minimum code with which to reproduce the problem I don't know how relevant the framework is that I used. But you should be able to follow the steps.
Watch what I do with r_f_Code (field code range). You can ignore/remove r_f_Result as I had that in for reference and debugging purposes.
Collapsing the field code range to its end-point leaves the range just within the field braces. Moving the starting point one character to the right puts it just outside the braces, but before anything else. (Note: I tested with two immediately adjacent SET fields.)
My code then enters some text and bookmarks it. That's the only way you do what you ask if what follows the SET field can be "anything". Although I suppose you could insert a Content Control - that would be uniquely identifiable if you go about it correctly...
Sub PositionAfterFieldCode()
Dim f As word.Field
Dim r_f_Code As word.Range, r_f_Result As word.Range
For Each f In ActiveDocument.Fields
If f.Type = wdFieldSet Then
Set r_f_Code = f.code
Set r_f_Result = f.result
'Debug.Print Len(r_f_Code), r_f_Code.Text, Len(r_f_Result), r_f_Result.Text
r_f_Code.Collapse wdCollapseEnd
r_f_Code.MoveStart wdCharacter, 1
'r_f_Code.Select
r_f_Code.Text = "abc"
r_f_Code.Bookmarks.Add "AfterSet", r_f_Code
Exit For
End If
Next
End Sub

Subform to search record on main form

I have a subform like so:
When i click the 'select' button, i wish for the record on the main form to navigate to the appropriate one (the one which holds the same Staff ID).
What I have tried so far is for when the button is pressed to run the following vba:
Private Sub Command6_Click()
Dim rs As Object
Dim strLinkValue As String
strLinkValue = Forms![navigation form]![NavigationSubform]![teacher search qry subform]![Staff ID].Value
Set rs = Forms![navigation form]![NavigationSubform]![teacher search qry subform].Form.RecordsetClone
rs.FindFirst "[Staff ID] = '" & strLinkValue & "'"
Forms![navigation form]![NavigationSubform].Bookmark = rs.Bookmark
End Sub
But when I do this I get run-time error 438 (object doesn't support this property or method).
Any ideas? I feel like I'm over-complicating things.
You first got error 438 ("Object doesn't support this property or method.") at this line ...
Set rs = Forms![navigation form]![NavigationSubform].Recordset.Clone
Changing from Recordset.Clone to Form.RecordsetClone cured the error (at that line).
Unfortunately you then got error 438 again when attempting to set Bookmark at this line ...
Forms![navigation form]![NavigationSubform].Bookmark = rs.Bookmark
The reason for the error at that point was because [NavigationSubform] is a subform control, and a control does not have a Bookmark property. You need to set the Bookmark on the Form contained within that control.
This code does what I believe you want. I tested it in Access 2010 with a copy of your database.
Dim rs As DAO.Recordset
Dim strLinkValue As String
strLinkValue = Me![Staff ID].Value
With Me.Parent.Form
Set rs = .RecordsetClone
rs.FindFirst "[Staff ID] = '" & strLinkValue & "'"
.Bookmark = rs.Bookmark
End With

MS Access: Link listbox to textbox

I have a textbox and listbox, one is for entering a new topic in a help form while the other looks up those new topics. I would like to be able to present the finished topics in both the textbox and listbox simultaneously to edit or lookup as well write new records in the help form. The listbox provides functionality to view which records there are now.
I find if I put nothing in and I go to a new record the prev/next buttons will stop working, maybe there is a control I need to add to keep it from freezing or to refresh? Normally I press esc to get out of a new record edit and return to others but that does not work as usual.
Or how else may I point to the listbox's current record source?
I currently have this code:
Private Sub List35_AfterUpdate()
DoCmd.GoToRecord acDataForm, "Help Form_Editor2", acGoTo, Me.List35.ListIndex + 1
Me.List35 = Me.List35.Column(0, Form.CurrentRecord - 1)
Dim index As Integer
index = Form.CurrentRecord - 1
Me.Text53 = Me.List35.Column(Me.List35.ListIndex + 1, index)
End Sub
I keep getting some of the items to read but others are null. I have about 8 items in the source table... what is going wrong? Why would there be nulls?
Another issue after getting this updated. When the code is setup the recordset starts at new when I allow additions and edits on the form. The code displays the list item as it should but the other items will not activate from the requeried listbox item. What might correct this issue?
Private Sub List35_AfterUpdate()
Dim myTitle As String
With Me.List35
If .ListIndex > -1 Then
'Use this one if you are using bound column
myTitle = .Column(1, Form.CurrentRecord)
'use this if you want something other than the bound column
'and you have more than one column in the list (hidden or not)
'nId = .Column(1, .ListIndex)
Me.RecordSource = "SELECT * FROM FormsHelpTable WHERE HelpTitle = '" & myTitle & "'"
Me.Text53.Value = myTitle
Else
Me.RecordSource = "SELECT * FROM FormsHelpTable WHERE HelpTitle IS NULL"
'Me.Text53.Value = "(New)"
End If
End With
Me.Requery
End Sub
This checks for ListIndex. It will be -1 if you don't have anything selected.
Private Sub List35_AfterUpdate()
Dim index As Integer
With Me.List35
If .ListIndex > -1 Then
DoCmd.GoToRecord acDataForm, "Help Form_Editor2", acGoTo, .ListIndex + 1
.Value = .Column(0, Form.CurrentRecord - 1)
index = Form.CurrentRecord - 1
Me.Text53 = .Column(.ListIndex + 1, index)
End If
End With
End Sub
I'm not sure what all your code is trying to do, so I didn't make any other adjustments other than to reduce all references to List35 to a single With statement.
I normally do something like this:
Private Sub List35_AfterUpdate()
Dim nId As Long
With Me.List35
If .ListIndex > -1 Then
'Use this one if you are using bound column
nId = .Value
'use this if you want something other than the bound column
'and you have more than one column in the list (hidden or not)
'nId = .Column(1, .ListIndex)
Me.RecordSource = "SELECT * FROM TableName WHERE Id = " & nId
Else
Me.RecordSource = "SELECT * FROM TableName WHERE Id IS NULL"
End If
End With
Me.Requery
End Sub

Filter form based on unbound control value

I have a form bound to a query, except for one field that I am leaving unbound. The idea is that the user will enter a value in that textbox and press a button to bring up the record. I have some code that I thought would work based on the interwebs. When I use DoCmd.ApplyFilter(filter_string) I get a popup asking for the value to filter on (which is not what I want). When I go ahead and paste it in, the form does not get filled. When I use Me.Form.Filter = filter_string, sometimes the form fills, but always with the same record, regardless of what the filter_string says. An example filter_string is
filter_string = "InventoryDetailsID = 'B01MFC000100/01'"
I have another similar form that, instead of filling with an existing query, generates the query (with 5 joins) and fills the form from the resulting recordset. It works just fine, but is slow because it has to run the query each time. That is why I want to use a method where I generate the query once, and then filter it.
Edit
Oh, and I also tried using a variant on the run-the-query-every-time approach, where I query the already generated query (the one I'm trying to filter). I'm using:
query_string = "SELECT * FROM qry_ISBN_All WHERE InventoryDetailsID LIKE '" & Me.InventoryDetailsID & "';"
But I get the error Run-time error '3061' Too few parameters, expected 1
Edit II
Private Sub btn_Seek_Click()
Dim temp As String
filter_string = "InventoryDetailsID = '" & Me.InventoryDetailsID & "'"
Me.temp = filter_string
Me.FilterOn = True
Me.Form.Filter = filter_string
Me.FilterOn = True
'DoCmd.ApplyFilter (filter_string)
' Dim query_string As String
' query_string = "SELECT * FROM qry_ISBN_All WHERE InventoryDetailsID LIKE '" & Me.InventoryDetailsID & "';"
End Sub
Typical filter string is given. It is printed to the form control Me.temp.
After this line:
Me.Filter = filter_string
Add this in:
Me.FilterOn = True
Also I agree, run the query every time approach is definitely overkill. The filter should provide you with the functionality you seek. You just simply have to "turn it on" after you set it.

Access fields in form using vba

I created a query and a form in Microsoft Access 2010. The form, named TEST, looks as follows:
Field1 Field2
a 200
b 400
In VBA I tried to access the different fields in the form:
Form_TEST.Field1....
I want to save the values 200 and 400 in an integer variable (Dim a As Integer) and print it using MsgBox. How can i achieve that??
You can use the Me as the open form and assign the variable if you know the name of the text box.
Dim intValue as Integer
'If text box name = TextBox1
intValue = Me.TextBox1.Value
I'll try to help you.
I understood you created the form with the wizard putting the 2 fields on the form.
What is not clear is the View that you are using.
Well, your form can be displayed in different ways:
- Single form
- Continuous forms
- Datasheet
This is defined by the Default View property.
To see the properties of you form press F4 to see properties and select "Form" as the object that you want to see.
If your form is Single Form or Continuous form you can access the two fields you put on it simply addressing them.
Click on the controls you put on the form and press F4 to see the control name.
CASE 1 - SINGLE FORM VIEW
Let's assume that your controls are named Text1 (200) and Text2 (400) and for convenience your form is a single form.
So you can refer to values in the 2 controls writing
Dim intText1 As Integer, intText2 As Integer
intText1 = Me.Text1.Value
intText2 = Me.Text2.Value
The .Value property is not mandatory cause it's the default property.
At this point you can print out intText1,2 with a MsgBox
MsgBox "Text1 = " & CStr(intText1)+ " - Text2 = " & CStr(intText2)
This will show Text1 = 200 - Text2 = 400
CASE 2 - CONTINUOUS FORMS VIEW
Let's now assume that your view is Continuous form.
So the field that contains 200 and 400 is just one but each record (row) is a form repeated as many times as the number of records.
In this case to access all the records and store them to an array you can use this in the Form_Load event (you can access it by the Control Properties Window - F4)
Option Explicit
Option Base 1 ' Set the base index for vectors to 1
Dim rst as DAO.Recordset ' Define a recordset to allocate all query records
Dim Values as Variant ' Define a variant to allocate all the values
set rst = me.RecordsetClone ' Copy all records in rst
rst.MoveLast ' Go to last record
intNumRecords = rst.RecordCount ' Count records
rst.MoveFirst ' Go back to recordset beginning
ReDim Values(intNumRecords) ' Resize Values to allocate all values
i = 1
Do While Not rst.EOF ' Cycle over all records
Values(i) = rst!FieldName ' FieldName is the name of the field of
' the query that stores 200 and 400
i = i + 1 ' Move to next array element
rst.MoveNext ' Move to next record
Loop
rst.Close ' Close recordset
set rst = Nothing ' Release memory allocated to rst
for i = 1 To intNumRecords ' Show easch value as message box
MsgBox Values(i)
next i
NOTES
Please NOte that this solution works if you have less than 32767 records to show (the maximum integer with sign that you can store).
The msgbox obliges you to press OK at each value. It's not so comfortable.
Please tell me if it's what you were looking for.
Bye,
Wiz