473,386 Members | 1,720 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,386 software developers and data experts.

Jumping to a specific record in the parent form

Hi,

I have an Access form ("Form1") with a button that opens another form ("Form2"). Form2 is not a subform. Form2 has a number of text controls that are unbounded. There is a button called "Commit". When it is clicked, it takes the values in the unbounded controls and inserts them into various tables. One of these is inserted into the source table as Member_Id.

Once the values are inserted, the following is supposed to happen:

1) Focus is set on Form1.
2) The Form1 Current record is set to the Member_Id that was inserted into its data source table.
3) Form2 closes leaving Form1 with the focus displaying the Member_Id record previously inserted.

I am able to do the record insertion, but I can't close Form2. Would anyone have an idea of what I need to do?

Here is the code I used:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''

Expand|Select|Wrap|Line Numbers
  1. Sub Commit_Record_and_Close()
  2.  
  3. Dim strCriteria As String
  4.  
  5. Dim rst As DAO.Recordset
  6.  
  7. ' Insert values including Member_Id into source table HERE.
  8. '
  9.     Forms("Form1").SetFocus
  10.  
  11.     Set rst = Forms("Form1").RecordsetClone
  12.  
  13.     strCriteria = "[Member_Id] = " & CStr(lng_Member_Id) ' Member_Id previously set.
  14.  
  15.     rst.FindFirst strCriteria
  16.  
  17.     Forms("Form1").Bookmark = rst.Bookmark
  18.  
  19.     Forms("Form1").Requery
  20.  
  21.     Forms("Form1").Controls("cbo_Selector").Requery ' This is a drop down that contains Member_Id
  22.  
  23.     ' The following 2 lines were inserted in desparation because I couldn't get Form1 to requery.
  24.  
  25.     DoCmd.OpenForm "Form1", acDesign
  26.  
  27.     DoCmd.OpenForm "Form1", acNormal, , strCriteria
  28.  
  29.     Me.SetFocus ' Form2
  30.  
  31.     DoCmd.Close acForm, Me.Caption, acSavePrompt ' Form2
  32.  
  33.  
  34. End Sub
Aug 2 '18 #1
2 1807
zmbd
5,501 Expert Mod 4TB
OK
The following, although written in the VBE, is essentially "air code" because I don't have your database.

Take a look at the code, back up your database, cut and paste this into your form's code, DOUBLE CHECK MY CODE for errors by trying to compile the data code from the debug menu.

+I'm sure that I've missed something here; however, impossible to tell without your help.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. '
  4. '>> You should have the above two lines in all of your modules and form codes.
  5. '>> These can be set by default under the VBE>Menu>Tools>Options
  6. '
  7.  
  8.  
  9. Sub Commit_Record_and_Close()
  10.  
  11.   Dim strCriteria As String
  12.   Dim rst As DAO.Recordset
  13.   '
  14.   ' Insert values including Member_Id into source table HERE.
  15. '
  16. '>> You have no code that would insert anything into a record set
  17. '>> at this point. I am guessing that you omitted this code for
  18. '>> clarity?
  19. '
  20.   '
  21. '>> Make sure that  your form is actually loaded
  22. '>> - unless you set the second form as dialog users tend to close things unexpectedly
  23.   If CurrentProject.AllForms("Form1").IsLoaded Then
  24.     Forms("Form1").SetFocus
  25.     Forms("Form1").Requery
  26.   Else
  27.     DoCmd.OpenForm FormName:="form1", View:=acNormal
  28.   End If
  29. '
  30. '>> You have to get your form to requery prior to setting your recordset based on the form's recordset
  31.   Set rst = Forms("Form1").RecordsetClone
  32.   strCriteria = "[Member_Id] = " & CStr(lng_Member_Id) ' Member_Id previously set.
  33.   '
  34.   rst.FindFirst strCriteria
  35.   '
  36. '>> YOU MUST check to see if there is a matching record before you attempt to move to that record.
  37.   If Not rst.NoMatch Then
  38.     Forms("Form1").Bookmark = rst.Bookmark
  39.     Forms("Form1").Controls("cbo_Selector").Requery ' This is a drop down that contains Member_Id
  40. '
  41. '>> sometimes a form will not update the interface so we'll force a redraw
  42.     Forms("Form1").Repaint
  43.     '
  44. '>> Remove these once we have things happy
  45.     ' The following 2 lines were inserted in desparation because I couldn't get Form1 to requery.
  46.     'DoCmd.OpenForm "Form1", acDesign
  47.     'DoCmd.OpenForm "Form1", acNormal, , strCriteria
  48. '
  49.     Me.SetFocus ' Form2
  50.   Else
  51.     MsgBox Prompt:="The expected record for " & vbCrLf & _
  52.       strCriteria & vbCrLf & "was not found" & vbCrLf & _
  53.       "Please contact the person responsible for this database", _
  54.       Buttons:=vbCritical + vbOKOnly, Title:="Error: Missing Record"
  55.   End If
  56.   '
  57. '>> RULE OF THUMB - If you Open it Close it, If you Set it Clear it
  58.   If Not rst Is Nothing Then
  59.     rst.Close
  60.     Set rst = Nothing
  61.   End If
  62. '>> You should close the record set BEFORE closing the record source
  63. 'The form caption will not work here, you have to have the form's actual name :)
  64.     DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name, Save:=acSaveNo   ' Form2
  65. '
  66. 'Error trapping?
  67. End Sub
Aug 2 '18 #2
PhilOfWalton
1,430 Expert 1GB
I'm sure there must be a reason for using 2 forms to enter your data, but I would be interested to find out the reason.

You mention that the data has to be entered into various tables. Usually the form is based on a query that includes those tables.

Don't forget that the data entered on a form isn't saved until one of several things happen like
1) Moving to a different record,
2) Closing the form,
3) Using a dedicated command button to "Save" the record.

Saving the record can be preceded by asking for confirmation that the data is OK.

Phil
Aug 2 '18 #3

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

Similar topics

25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
5
by: tdmailbox | last post by:
I have a form with a child form. In the child form there is a list of names that can grow quite large. On the parent form I want to display the first name from the child form. I set up a test...
2
by: dbuchanan | last post by:
Hello, I want to open a child form based on the record selected in a dataGridView of the parent form. I want the child form to open with a populated child table based on the selected parent...
1
by: adamj | last post by:
Hi people... Is it possible to, by using any means...use a button to open up a form on a particular record? If so how?
13
by: bitsnbytes64 | last post by:
Hi, I have a form which contains a subform. Both are were creetd using the form wizard and are bound by the column IXO_NR (on two different tables), which is the control source for a textbox on...
1
by: Kevin Welch | last post by:
Looking for some expert help here. Caveat: I do not know SQL, and I've built a database basically by trial and error (unfortunately, I don't have much time to learn Access as I would want). ...
3
by: DavidB | last post by:
I want to be able to go to a specific record on my form when I open it. The record I want to go to will be dynamic based on the value currently stored in a global variable. The global contains...
0
by: Jason C | last post by:
I've got a simple database in Access 2007 with a form interface. One of the forms contains subforms and uses multiple tables, currently navigation works fine. There's also several complex queries...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.