Use field content to reference a field on another form

Rachael

Registered User.
Local time
Today, 04:10
Joined
Nov 2, 2000
Messages
205
Hi All,

This is probably a simple question but can't for the life of me work it out at the moment.

I have a form with unbound fields on it which is used to create new records.

This form is going to be used on a touch screen so I have made another form that is a popup keypad.

The keypad will be used to enter numbers for several different fields on the main form. I know I could easily make this work by having several different version of the keypad referencing these fields but I'm trying to be clever and make it abit more dynamic.

So far my idea is to have a little command button next to each unbound field on the main form so when the user wants to enter a number on the first field the button is clicked and the keypad will record in an unbound field the reference ie Forms!MainForm!Field1

The number is then entered on the keypad (by using a series of command buttons) and is stored in another unbound field called KeypadNo

Then I thought the user would click a button to send the newly entered number from the keypad back to the main form and it would know which field to enter it into because the form reference ie Forms!MainForm!Field1 was stored on the keypad when it was opened.

Aaagh, hope this makes sense, I just can't figure out how to get the contents out of the form reference field so it know where to copy to on the main form.

thanks, Rachael
 
I think you could use the OpenArgs of the DoCmd that opens the keyboard form to let that form know which field to send the numbers back to.
 
Check out this Thread and have a look at the links that Uncle Gizmo suggests.
 
Hi John,

Have implemented Uncle Gizmo's keypad and it works great when the form fields have a control source. My form is using unbound fields that are eventually used in an append query, so when I try to use the keypad I get "Invalid use of Null" which I think I've narrowed down to the unbound field not passing the control name.

I think its in this class module but I'm not knowledgable enough with code to manipulate it:


Option Compare Database
Option Explicit
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'>>> Database by Tony Hine, alias Uncle Gizmo <<<
'>>> Created Oct, 2007 <<<
'>>> Last up-dated Dec 2007 <<<
'>>> Telephone International: +44 1635 522233 <<<
'>>> Telephone UK: 01635 533322 <<<
'>>> e-mail: email@tonyhine.co.uk <<<
'>>> Skype: unclegizmo <<<
'>>> MSN messenger: walter.tower@lycos.co.uk <<<
'>>> I post at the following forum (mostly) : <<<
'>>> http://www.access-programmers.co.uk/forums/ (alias Uncle Gizmo) <<<
'>>> You can also find me on the Ecademy: http://www.ecademy.com/user/tonyhine <<<
'>>> If my e-mail don't work, try this website: http://www.tonyhine.co.uk/example_help.htm <<<
'>>> I have now started a forum which contains video instructions here: <<<
'>>> http://msAccessHintsAndTips.Ning.Com/ <<<
'>>> NOT CHECKED AND TESTED FOR ERRORS!!!! Be Warned <<<
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

'This Class Can be used anywhere and returns the name of the Screen Active Control, the Parent Form and other info

'It is also part of a pair of items:
'Both are needed for the calendar form to work
'clsGetActiveFrm (This Class)
'frmCalendar

'Copy the class "clsGetActiveFrm" and the form "frmCalendar" Into your project.
'You will also need to make sure your project has a reference to.....
'
'Reference:
'Microsoft visual basic for applications extensibility 5.3 (or the one relevant to your version of MS Access)

'Usage
'Dim GetActiveFrm As New clsGetActiveFrm
'
' Msgbox "Active Control : " & GetActiveFrm.prpScrActiveCtrl
' Msgbox "Date Control : " & GetActiveFrm.prpAssociateCtrl
' Set GetActiveFrm = Nothing

'Declare the private variables used in this Class
Private mAssociateFrm As Form
Private mHasPBkProc As Boolean
Private mstrScrActiveCtrl As String
Private mctlAssociateCtrl As Variant

Public Property Get prpAssociateFrm() As Form
Set prpAssociateFrm = mAssociateFrm
End Property 'prpAssociateFrm Get
Public Property Get prpHasPBkProc() As Boolean 'True if Function Named "fPassBackAct" is Found
prpHasPBkProc = mHasPBkProc
End Property 'prpHasPBkProc Get

Public Property Get prpScrActiveCtrl() As String
prpScrActiveCtrl = mstrScrActiveCtrl
End Property 'prpScrActiveCtrl Get

Public Property Get prpAssociateCtrl() As Variant
Set prpAssociateCtrl = mctlAssociateCtrl
End Property 'prpAssociateCtrl Get

Private Sub Class_Initialize()
On Error GoTo Err_ErrorHandler

If fAnyOpenFrms = False Then GoTo Exit_ErrorHandler 'Check if any Forms are Open, Exit if none are open...

Dim ctlCurrentControl As Control
Set ctlCurrentControl = Screen.ActiveControl 'Get the Control that is Active on the Form.

mstrScrActiveCtrl = ctlCurrentControl.Name 'now Get the Name of the Control

Dim ctlToTest As Control
Set ctlToTest = ctlCurrentControl
Dim X As Integer

For X = 1 To 20 'This will check to 20 levels
If Not fParentIsaForm(ctlToTest.Parent.Name) Then 'Is Parent a Form?
Set ctlToTest = ctlToTest.Parent 'No --- Then check the next Parent
Else
Set mAssociateFrm = ctlToTest.Parent 'Yes -- Then Set the Property to The Parent Form
Exit For
End If
Next X

mHasPBkProc = fProcExists(prpAssociateFrm, "fPassBackAct") 'True if Form has the Function "fPassBackAct" present

Dim strAssociateCtrl As String
If ctlCurrentControl.ControlType = acCommandButton Then 'Look for control named simarlarly to the Command button
strAssociateCtrl = fTextBoxName(prpScrActiveCtrl, "txt")
Else
strAssociateCtrl = ctlCurrentControl.Name 'Otherwise you've clicked on yourself (Not a Command Button)
End If

If fChkName(prpAssociateFrm, strAssociateCtrl) Then
Set mctlAssociateCtrl = prpAssociateFrm(strAssociateCtrl) 'Returns the Textbox as a Control to the Property
End If

Exit_ErrorHandler:
Set ctlCurrentControl = Nothing
Exit Sub

Err_ErrorHandler:
Select Case Err
Case 1 'Example of Trapping Error 1 --- Not sure there is an Error 1 I haven't seen it
MsgBox "produced by error code (1) please check your code ! Error Number >>> " _
& Err.Number & " Error Desc >> " & Err.Description
Case Else
MsgBox "Error From --- clsGetActiveFrm Class_Initialize --- Error Number >>> " _
& Err.Number & " <<< Error Description >> " & Err.Description
End Select
Resume Exit_ErrorHandler
End Sub 'Class_Initialize

Private Function fParentIsaForm(strIsaForm As String) As Boolean
'From Access Help
fParentIsaForm = False
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
' Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
If obj.Name = strIsaForm Then fParentIsaForm = True
Next obj
End Function 'fParentIsaForm

Private Function fTextBoxName(strCboName As String, strPreFix As String) As String
'Builds the anticipated name of the Textbox from the Combobox name by striping out the name of the Command Button
'from a Text string like "btnDOB" and then Prefixes it with "txt"
Dim strNamePart As String
strNamePart = Right(strCboName, Len(strCboName) - 3)
fTextBoxName = strPreFix & strNamePart
End Function 'fTextBoxName

Private Function fAnyOpenFrms() As Boolean
'Code from Access Help >>> If any Forms are open Return True, if no Forms open Return False
Dim blnOpen As Boolean
Dim intCounter As Integer
blnOpen = False
fAnyOpenFrms = False
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
' Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
If obj.IsLoaded = True Then
intCounter = intCounter + 1
End If
Next obj
If intCounter > 1 Then fAnyOpenFrms = True
End Function 'fAnyOpenFrms

Function fProcExists(frmPassed As Form, strProc As String) As Boolean
'Usage >>> This message box will return true if Form "Form2" (Must Be Open) contains a procedure "fTest"
'MsgBox fProcExists("Form2", "fTest")

On Error GoTo Err_ErrorHandler
Dim intCounter As Integer
fProcExists = True
If frmPassed.HasModule = False Then
fProcExists = False
Exit Function
End If
'/////// May need to Check to see if the Form is Open
intCounter = frmPassed.Module.ProcCountLines(strProc, vbext_pk_Proc)
'This counter returns the position (Line number) of the procedure in the module, however if the procedure
'does not exist in the module, then error 35 is thrown and "fProcExists" set to False
'I realize this is not considered good practice, using an error thrown to check for something,
'however I still am looking for a way of doing it by looping through the names of the procedures
'in the form module. Haven't found a way to do that yet!

Exit_ErrorHandler:
Exit Function

Err_ErrorHandler:
Select Case Err
Case 35 'If Sub or Function not Defined then Return False
fProcExists = False
Case 2450 'Form not open
MsgBox "Form >>> " & frmPassed.Name & " <<< Is probably not open or does not Exist"
fProcExists = False
Case Else
MsgBox "Error From --- fProcExists --- Error Number >>> " _
& Err.Number & " <<< Error Description >> " & Err.Description
fProcExists = False
End Select
Resume Exit_ErrorHandler
End Function 'fProcExists

Private Function fChkName(frmPassForm As Form, strChkName As String) As Boolean
'Check to see if there is a Textbox with the same name as the Command Button ("txt" as the first three characters)
'If there is a Textbox with the same name as the Command Button Return True, otherwise Return False
fChkName = False
Dim Ctl As Control
Dim intCounter As Integer
For Each Ctl In frmPassForm.Controls
If Ctl.ControlType = acTextBox Or Ctl.ControlType = acComboBox Then 'Find text boxes only
If Ctl.Name = strChkName Then
intCounter = intCounter + 1
End If
End If
Next Ctl
If intCounter = 1 Then fChkName = True
End Function 'fChkName


Thanks to anything who has a squiz at this and can help me on the right track.

Kind regards,

Rachael
 
This is well out of my league, I'm hoping Uncle Gizmo will be along soon to give you a hand.
 
Thanks for taking a look John, I appreciate your help.

Kind regards, Rachael
 
Hi me again,

I don't think the above posted code is the problem anymore so I have attached the sample db I got the keypad and modules from.

When I imported the Bookings form and all relevant gear the Keypad worked beautifully. When I applied the same method to my form I kept getting Invalid use of Null when I clicked on a letter on the Keypad.

The only difference I could initially detect was that the Bookings form had bound fields and my form Im using didn't have bound fields. ?????

Incidentially now since trying to decipher what's going on the Bookings form is now not working either "Object doesn't support property or method"

I've buggered something up somewhere!
 

Attachments

I solved it! and for those with the same prob, here it is!!

I added this into the clsGetActiveFrm I think anyone who wants to know where can figure it out

If fChkName(prpAssociateFrm, strAssociateCtrl) Then
If IsNull(prpAssociateFrm(strAssociateCtrl)) Then ' my added bit
prpAssociateFrm(strAssociateCtrl) = "" ' my added bit
Set mctlAssociateCtrl = prpAssociateFrm(strAssociateCtrl) ' my added bit
Else ' my added bit
Set mctlAssociateCtrl = prpAssociateFrm(strAssociateCtrl)
End If
End If

Basically from my poor knowledge of VBA I figured out that because the field was unbound it was coming in as Null ie not even nothing so the IF statement set it to nothing then away I went. Well this is what I think I understand anyway.

Cheers, Rachael
 
Hi Uncle Gizmo,

Just saw your post now, I was too busy typing up my triumph to notice!!

If your were over here in Oz you'd not be off to work but looking forward to Beer'O'clock....half an hour to go

Thanks for your reply, Cheers, Rachael
 
Hi Uncle Gizmo if you're still out there,

(Maybe we're all 'out there' and that's the problem)

I just have had a little prob with the keypad code on another computer that has 2003 access but uses 2007 runtime to run my database. Generally I don't have any problems with this set up.

whats happening... I'm getting an error message 50289 in fProcExist

The keypad works fine on my main computer but doesn't like the other one. When testing I convert the db to accde then change the extension to accdr, disable shift etc etc.

do you know what this means?

thank you very kindly for your help so far

Rachael
 
Hi Uncle Gizmo,

I can assure you the keypad works fine on my computer which is using 2007 and I have referenced the VBA extens...5.3 reference, it's this other computer that has full version of 2003 and 2007 runtime. Would the reference order make any difference? I also have Mircosoft scripting runtime, Windows Script host object, Graph 12.0 referenced

sorry for asking but I can't personally access this other computer as its one of my employees who's not in the same area so I have to work blind and keep trying things to clear the problem up. A real pain in the bum!

Thanks, Rachael
 
Thanks Uncle Gizmo that was my thought but as always one hopes that a miracle might occur and the answer will magically appear. I will post back when I find a solution to let you know how I got on.

cheers, Rachael
 

Users who are viewing this thread

Back
Top Bottom