473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

A subroutine to return user to specific row in subform

Hi Gang,

I'm trying to write a public subroutine that returns a user to a specific row in a subform. Here's what I have so far...
Expand|Select|Wrap|Line Numbers
  1. Public Sub psubGoToRecordInSubform(ctrSubForm As Control, intID As Integer, strIDField As Object)
  2. 'returns cursor to a specific record in a subform
  3.  
  4.       Dim rs As Object
  5.       Dim lngBookmark As Long
  6.  
  7.       'set a variable to ID
  8.       lngBookmark = intID
  9.  
  10.       DoCmd.GoToControl ctrSubForm.Name
  11.  
  12.       'take it to the selected record
  13.       Set rs = ctrSubForm.Form.RecordsetClone
  14.       rs.FindFirst strIDField & " = " & lngBookmark
  15.       ctrSubForm.Form.Bookmark = rs.Bookmark
  16.  
  17.       Set rs = Nothing
  18. End Sub
The problem occurs on the second to last line:

rs.FindFirst strIDField & " = " & lngBookmark

I can't seem to get the program to pick up strIDField properly. I've tried declaring it as a string, an object. I declared it as a control and used strIDField.Name and it errored. Access message said "Actual field name" is not a recognized field. This makes me wonder it this line is doing it's job:

DoCmd.GoToControl ctrSubForm.Name

Does anyone know what I'm doing wrong? (I mean besides trying to be a programmer :) )

Thanks,
Adam
Nov 11 '09 #1
4 2586
Delerna
1,134 Expert 1GB
Have you tried putting breakpoints on the code and running it.
When the breakpoint occurs hover you mouse over the variables to see what they contain.

Also, maybe you can try

docmd.FindRecord
Nov 11 '09 #2
ADezii
8,834 Expert 8TB
@AdamOnAccess
I don't play around with Bookmarks much, but couldn't you:
  1. Declare a Global Variable to hold the Value of the Sub-Forms Bookmark:
    Expand|Select|Wrap|Line Numbers
    1. Public strBookmark As String
  2. Set the Bookmark in the Sub-Form at some convenient point:
    Expand|Select|Wrap|Line Numbers
    1. strBookmark = Forms![Orders]![Orders Subform].Form.Bookmark
  3. Later, set Focus to the Sub-Form, then return to the previously set Bookmark:
    Expand|Select|Wrap|Line Numbers
    1. Forms![Orders]![Orders Subform].SetFocus
    2. Forms![Orders]![Orders Subform].Form.Bookmark = strBookmark
  4. I tested this on the Order Details Sub-Form of the Sample Northwind Database, and it appears to work quite well.
Nov 12 '09 #3
Stewart Ross
2,545 Expert Mod 2GB
I'd use ADezii's solution - it is neat and simple.

If you want to continue with findfirst, you mention that you have tried using the Name property of the control concerned. This will only work if the control name is identical to the name of the underlying field in your recordset to which it is bound. If it is (and by default it should be, unless you have renamed the control), be careful about the use of spaces, as these will prevent Access from recognising the name of the field unless you use the rectangular brackets form as shown below, or you replace the spaces with underscores.

Using the name property of the control your expression should be something like this:

Expand|Select|Wrap|Line Numbers
  1. rs.FindFirst "[" & strIDField.name & "] = " & intID
You can confirm all this by stepping through your code line-by-line and using the immediate window to test values, as Delerna suggested.

I have used intID directly instead of assigning it to lngBookmark as you did; bookmarks are specific properties of recordsets and are not ordinary integers or the like. Confusing the two is not a good idea.

-Stewart

PS strIDField is currently defined as an Object. If you are passing a control to it then define it as a Control data type - at least then you will have a Name property to access, and Intellisense will be able to show you the properties of the control when you wish to see them in the immediate window.
Nov 12 '09 #4
NeoPa
32,556 Expert Mod 16PB
Your use of strIDField is quite wrong and the name misleading. It should be a string variable, and the name passed. When used, for ensuring it will always work, I suggest you enclose the name value passed in [] characters to ensure that any spaces or other questionable characters in the name do not cause the SQL engine to get confused.

Also, As the word Bookmark has a special meaning in Access I would avoid usage of it for a simple ID variable. Perhaps simply passing the ID in as lngID in the first place would tidy up your code and remove this misleading variable entirely.
Expand|Select|Wrap|Line Numbers
  1. Public Sub psubGoToRecordInSubform(ctrSubForm As Control, _
  2.                                    lngID As Long, _
  3.                                    strIDField As String)
  4. ...
  5. rs.FindFirst "[" & strIDField & "]=" & lngID
Nov 19 '09 #5

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

Similar topics

1
by: CFW | last post by:
I have read as much as I could find here and in Tony's site as well - being directed there from here. I have a three user database with a BE .mdb (Access 2K) in a share on one PC and each user...
0
by: CSDunn | last post by:
Hello, In Access ADP's that connect to SQL Server databases, any time I have a situation where I have a combo box in a main form that looks up a record in a subform, the subform record source has...
5
by: Richard Hollenbeck | last post by:
I have a form that was a stand-alone form which I converted into a sub-form. On the original form was a close button which did a lot more than just close. It ran update and append queries (after a...
5
by: keith | last post by:
This may seem simple, but I'm having a bit of trouble figuring out exactly how to do it. I'm accessing a database through an ODBC link, and I have a query that returns only jobs completed that day...
7
by: OutdoorGuy | last post by:
Greetings, I'm still relatively new to C# and was wondering if there was a way to exit a subroutine (such as "Exit Sub" in VB)? I have the code below which performs validations. If the user...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
14
by: bgreenspan | last post by:
I have a form with a subform. In the subform I have several fields to which I am assigning links to documents on the file server. I learned (from this list :-) how to add the links. Because I...
10
by: nasau | last post by:
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the...
1
by: peterv6 | last post by:
I'm using a "package" type subroutine, called test_package.pl. I'm calling it from a script called split0.pl. I want to pass the $0 variable, use the subroutine to split out just the filename, and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.