Results 1 to 5 of 5
  1. #1
    j9070749 is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2011
    Posts
    20

    IF stDocName

    I have a form, which is also used as a subform. The code needs to be changed depending on which form it is opened in. I have tried some code which is shown below, but bring up the error 'can't find the form stdocname'. Can anyone help please?

    Thanks


    Dim stDocName as string

    if formname ="[Records]" then stDocName = "[Records]"


    else
    stDocName = "[Demogrpahics]"

    ID = [Forms]![stDocName]![Patient ID Number]

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by j9070749 View Post
    I have a form, which is also used as a subform. The code needs to be changed depending on which form it is opened in. I have tried some code which is shown below, but bring up the error 'can't find the form stdocname'. Can anyone help please?

    Thanks


    Dim stDocName as string

    if formname ="[Records]" then stDocName = "[Records]"
    else
    stDocName = "[Demogrpahics]"

    ID = [Forms]![stDocName]![Patient ID Number]
    There are a few problems:

    1) "formname" has not been declared (ie DIM formname as ????) Or is "formname" a control on a form?? If so, then it is better to use "Me.formname" (without the quotes)

    2) "stDocName" is a variable, so of course [Forms]![stDocName]![Patient ID Number] will cause an error. ("stDocName" is not the name of a form)

    3) The syntax for the IF() function is wrong. There are two forms of the IF() function. One is the single line form; "ELSE" is not allowed (try to avoid this form). The other form requires "END IF" to terminate the function.

    Without knowing the rest of the code, you could try:

    Code:
    Dim stDocName as string 
     
    IF Me.formname ="[Records]" then 
       stDocName = "[Records]"
       ID = [Forms]![Records]![Patient ID Number]  ' what is ID??? a control or a variable??
    ELSE 
        stDocName = "[Demogrpahics]"
    END IF

  3. #3
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    I'm not on my Access computer, but how about trying....

    Quote Originally Posted by j9070749 View Post
    I have a form, which is also used as a subform. The code needs to be changed depending on which form it is opened in. I have tried some code which is shown below, but bring up the error 'can't find the form stdocname'. Can anyone help please?

    Thanks


    Dim stDocName as string

    if formname ="[Records]" then ID = [Forms]![Records]![Patient ID Number]
    else
    ID = [Forms]![Demographics]![Patient ID Number][/QUOTE]

    Also, i'm not sure about your reference to "formname". Use debug.print or Msgbox to be sure that "formname" returning the values for which you are trying to test.

  4. #4
    j9070749 is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2011
    Posts
    20
    Sorry I will try and be more clear. There is a demogaphics form where you can change a persons physio. The code for this is:


    Private Sub Date_AfterUpdate()
    Dim pid As Long
    Dim rst As Recordset
    Dim dbs As Database
    Dim str As String

    pid = [Forms]![Demographics]![Patient ID Number]

    str = "SELECT * FROM [Patient Physio Details] WHERE [Patient ID Number]=" & pid & " AND Format([Date], ""mm/dd/yyyy"")=#" & Me!Date & "#;"
    Debug.Print str
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset(str)

    If rst.RecordCount > 0 Then
    MsgBox "There is already a Physio specified on the same date, for the specific patient." & vbNewLine & "Please enter a different date."
    Me!Date = Null
    Me.Date.SetFocus
    End If


    This works fine in, but when it is on a subform it brings up a messege saying can't find the subform 'demographics'. If i change the code to

    pid = [Forms]![Physio Record]![Patient ID Number]

    It works fine in the subform but does not work in the form. So depending on where the form is opened, depends on which code it needs to run properly.

    Hope this makes things more clear, and thanks for you help already.

  5. #5
    j9070749 is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2011
    Posts
    20
    Thanks from your help I have managed to solve this problem!

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums