Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2013
    Posts
    14

    Unanswered: Alter code to use field found in another Form

    Using the code below in Access 2010 works great with the current Form I've been using.

    Code:
    Private Sub Command1043_Click()
    
    On Error GoTo Err_cmdExplore_Click
    
    Dim stAppName As String
    
    stAppName = "C:\Windows\explorer.exe D:\Clients\" & Me.txtContactName & "\"
    Call Shell(stAppName, 1)
    If Dir("D:\Clients\" & Me.txtContactName & "\") = "" Then
    MsgBox "Folder not found."
    Else
    Exit_cmdExplore_Click:
    Exit Sub
    
    Err_cmdExplore_Click:
    MsgBox Err.Description
    Resume Exit_cmdExplore_Click
    
    End If
    
    End Sub
    I decided to move the button to a different Form, and now it fails because it can't find the txtContactName field.

    The new Form I moved it to, does not have the field, txtContactName. How can I rewrite this so it can find the txtContactName field from the Form that does have it?

    The name of the Form that has the field is named Main and the new Form I moved it to is named ContactDetails.

    I assume I need to change this line of code but not sure how to accomplish this:
    Code:
    stAppName = "C:\Windows\explorer.exe D:\Clients\" & Me.txtContactName & "\"

    Thank you
    Mike

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    'me' refers to the current form
    you can specify another form, say it was called myotherform

    Code:
    stAppName = "C:\Windows\explorer.exe D:\Clients\" & forms!myotherform!txtContactName & "\"
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Feb 2013
    Posts
    14
    Quote Originally Posted by healdem View Post
    'me' refers to the current form
    you can specify another form, say it was called myotherform

    Code:
    stAppName = "C:\Windows\explorer.exe D:\Clients\" & forms!myotherform!txtContactName & "\"
    Thanks, you made it sound easy! I tried it though and it didn't work for me.

    I got a new error:
    MS Access can't find the field 'txtContactName' referred to in your expression.

    Here is my changes:
    Code:
    Private Sub Files_Click()
    
    On Error GoTo Err_cmdExplore_Click
    
    Dim stAppName As String
    
    stAppName = "C:\Windows\explorer.exe D:\Clients\" & Forms!main!txtContactName & "\"
    Call Shell(stAppName, 1)
    If Dir("D:\Clients\" & Forms!main!txtContactName & "\") = "" Then
    MsgBox "Folder not found."
    Else
    Exit_cmdExplore_Click:
    Exit Sub
    
    Err_cmdExplore_Click:
    MsgBox Err.Description
    Resume Exit_cmdExplore_Click
    
    End If
    
    End Sub
    Main is the Form name that the code works in, so I'm not sure why when I follow your directions, it fails to find it in the new Form.

    Also does the 'Main' Form have to be open for this to work? I ask because, when I do not have 'Main' open, I get the following error:
    MS Access cannot find the referenced form 'Main'.

    Any suggestions?
    Mike
    Last edited by Modify_inc; 03-25-13 at 17:06.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    yes the form has to be loaded, how else do you think its going to find the value of a control on that form?
    put in some error trapping
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Feb 2013
    Posts
    14
    Quote Originally Posted by healdem View Post
    yes the form has to be loaded, how else do you think its going to find the value of a control on that form?
    put in some error trapping
    You are talking to me like I should know what I'm doing. Let me make it clear, I don't. I'm trying to learn and understand. At this very moment, I paused a video that is trying to teach me some of the basics of Access and VBA programming.

    I just want to reaffirm that you acknowledged that the change you suggested did not work, whether the Main form is open or not.

    My understanding was that each form or object did not have to be open to make use of it from other forms. Just as my tables are not opened, but the forms work fine accessing the data from them. I'm sure there is more to it than that, I just wanted to provide you how I envisioned it.

    I will love to put in some error trapping, once I learn exactly what that means and how to properly implement it into my code.

    For now though, do you have any suggestions to why the code you suggested did not work? If the code I provided works in the Main form, why would it not work in the ContactDetails form when making the changes you suggested?

    Thank you
    Mike

  6. #6
    Join Date
    Jul 2005
    Posts
    13
    You may have to insert code after Dim stAppName As String that says DoCmd.OpenForm, "Contact Details" - you can set it to Visible=False if you don't want it to be visible. Then your reference to txtContactName on that form will work.

  7. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Quote Originally Posted by Modify_inc View Post
    My understanding was that each form or object did not have to be open to make use of it from other forms. Just as my tables are not opened, but the forms work fine accessing the data from them. I'm sure there is more to it than that, I just wanted to provide you how I envisioned it.
    Unfortunately, Access does not work that way and your understanding is wrong. You cannot access any control, property, method of a Form object when this Form object is closed. To make an analogy with a text file, you cannot read the contents of a text file unless you open this file first. The comparison with the tables is irrelevant. Moreover, you only can read the contents of a table through a RecordSet, which is a technique for opening it, explicitly (db.OpenRecordset) or implicitly (use of the RecordSource or RowSource methods, for instance).
    Have a nice day!

  8. #8
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    its is slightly disconcerting to realise you are using code without understanding what the code does
    Code:
    On Error GoTo Err_cmdExplore_Click
    is error trapping.

    can we see the actual code that is failing
    the error message that is reported
    the value if any of stAppName

    failing that zip up a compressed desensitised copy of your db and I or someone else will have a look at it.
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •