Connecting Tech Pros Worldwide Forums | Help | Site Map

Help: change active connection from one form to another

Newbie
 
Join Date: Sep 2006
Posts: 16
#1: Nov 6 '07
Dear expert,

I got two forms.

form1: Combox form Form_rptStock (key field: serial number), with one command button.
form2: Entryform Entryform_stock, with a series of command buttons. I use this form to add new record or edit/delete a record. (it works perfect independently)

What I am trying to do is when I select a serial number from the dropdown and click the command button cmdEdit in the 1st form, I wish the 2nd form will open and then, run the procedures I choose from the 2nd form. My problem is that the active connection is still in the first form, so it wil not recogernise or run the code in the 2nd form. How could I change the active connection to the second form when it is opened?

I got some brief code as in the follwoing:

code 1: to open the second form
Expand|Select|Wrap|Line Numbers
  1.     Dim stDocName As String
  2.     stDocName = "entryform_Stock"
  3.     DoCmd.OpenForm stDocName, , acNormal
  4.     cmdEdit_Click (----procedure in the 2nd form I wish to run)
  5.  
Code 2: cmdEdit_click () (only part of the whole code)
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdEdit_Click()
  2.  On Error GoTo err
  3.  Dim cnn As ADODB.Connection
  4.     Dim rst2 As Recordset
  5.     Set rst2 = New ADODB.Recordset
  6.     Set cnn = CurrentProject.Connection
  7.     rst2.ActiveConnection = cnn
  8.     rst2.CursorType = adOpenKeyset
  9.     rst2.LockType = adLockOptimistic
  10.     Dim boolInTrans As Boolean
  11.     boolInTrans = False
  12.         cmdDelete.Enabled = False
  13.         cmdAdd.Enabled = False
  14.  
  15.     If cmdEdit.Caption = "Edit" Then
  16.      '   UnLocked
  17.      cnn.BeginTrans
  18.      boolInTrans = True
  19.         Dim key As String
  20.         key = InputBox("Enter Serial Number ")
  21.  
  22.  
  23.         rst2.ActiveConnection = CurrentProject.Connection
  24.         rst2.CursorType = adOpenStatic
  25.         rst2.LockType = adLockOptimistic
  26.  
  27.         rst2.Open "select * from tblStock"
  28.  
  29.         strSql = "Serial_Number=" & key
  30.         rst2.Find strSql
  31.  
Any help is highly appreciated.

Jay

Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#2: Nov 7 '07

re: Help: change active connection from one form to another


Please use code tags.

Why not just put the sub/function in a global module and call the function from multiple places? You can't do what you want because the form's are out of each other's scope so they can't ever run each others functions directly.
Newbie
 
Join Date: Sep 2006
Posts: 16
#3: Nov 8 '07

re: Help: change active connection from one form to another


Quote:

Originally Posted by Rabbit

Please use code tags.

Why not just put the sub/function in a global module and call the function from multiple places? You can't do what you want because the form's are out of each other's scope so they can't ever run each others functions directly.


Dear Rabbit,

Many thanks for your reply.

Do you mean I can create a new moduel, copy and paste the codes into the created moduel, and then call the function when it is needed? please pardon me if I ask a stupid question as I am quite new with Access. Would you mind to give me a quick sample?

Looking forward to hearing from you.

Kindest regards,

Jay
Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#4: Nov 8 '07

re: Help: change active connection from one form to another


Quote:

Originally Posted by Jiwei06xie

Dear Rabbit,

Many thanks for your reply.

Do you mean I can create a new moduel, copy and paste the codes into the created moduel, and then call the function when it is needed? please pardon me if I ask a stupid question as I am quite new with Access. Would you mind to give me a quick sample?

Looking forward to hearing from you.

Kindest regards,

Jay

That's exactly what I mean.

Module1
Expand|Select|Wrap|Line Numbers
  1. Public Function SomeFunction()
  2.    MsgBox "Test"
  3. End Function
  4.  
Form1
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtBox_Cick()
  2.    Call SomeFunction()
  3. End Sub
  4.  
Form2
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtBox1_Cick()
  2.    Call SomeFunction()
  3. End Sub
  4.  
Reply