Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513

    changing a forms record source and control sources in code

    I have a simple single record form that can be used to display data from one of several different tables, where each table has different field names for the same info
    'its inherited, and one day, I'll normalize(?) this, but UGH, not this go around...
    ---> the question is simple: IS THERE A MORE ELEGANT WAY TO DO THIS?



    Code:
    Public Sub ANotes_SourceUpdate(vNoteRef As Long, vRecSource As String)
    Dim strFormName As String
    strFormName = "frmANotes_Edit"
    Dim frm As Access.Form
    
    
        Select Case vRecSource
        Case Is = "GNote"
            strTbl = "tbeGenrlNotes"
            strNoteRef = "OptNumber"
            strTitle = "AFill_Title"
            strText = "AFill_Text"
        Case Is = "CNote"
            strTbl = "tbeCoordNotes_EOS"
            strNoteRef = "CNote_ID"
            strTitle = "CNote_Title"
            strText = "CNote_Text"
        Case Is = "Installation"
            strTbl = "tbeInstallNotes_EOS"
            strNoteRef = "InstallNote_ID"
            strTitle = "InstallNoteTitle"
            strText = "InstallNoteText"
        End Select
    
    ' if i understand correctly, the form has to be open to alter the record source.... seems suspect.... IS THIS CORRECT?
        DoCmd.OpenForm strFormName, acNormal
    
      
    ' Set form properties.
        Set frm = Forms!frmANotes_Edit
        With frm
            vSelect = strTbl & " where " & strNoteRef & " = " & vNoteRef
            .RecordSource = "SELECT * FROM " & vSelect
            .Form.txtNoteTitle.ControlSource = CNoteTitle
            .Form.txtNoteText.ControlSource = CNoteText
        End With
    
        frm.Refresh 
    ' this didn't work, nor did: requery, repaint, ...
    
    ' the only thing I could get to work is to close the form, and then re-open it, which seems CLUNKY
    ' ***  this is really the question: IS THERE A MORE ELEGANT WAY TO DO THIS? ***
    
        DoCmd.Close acForm, strFormName
        DoCmd.OpenForm strFormName, acNormal
    
    
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Probably doesn't work because code is not referencing variables to set ControlSource property.
    Code:
    ' Set form properties.
        Set frm = Forms!frmANotes_Edit
        With frm
            vSelect = strTbl & " where " & strNoteRef & " = " & vNoteRef
            .RecordSource = "SELECT * FROM " & vSelect
            .Form.txtNoteTitle.ControlSource = strTitle
            .Form.txtNoteText.ControlSource = strText
        End With
    I have done something like this. However, I put code to set form properties in that form Open event.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 2
    Last Post: 07-13-2018, 05:44 AM
  2. Replies: 12
    Last Post: 12-10-2013, 08:57 PM
  3. Replies: 5
    Last Post: 09-18-2013, 09:15 PM
  4. Replies: 7
    Last Post: 10-28-2012, 02:55 PM
  5. one form two sources - invalid control source msg
    By techexpressinc in forum Forms
    Replies: 1
    Last Post: 05-28-2010, 02:11 PM

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