Manually Selecting Access Table from a User Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SteveG2013
    New Member
    • Apr 2015
    • 2

    Manually Selecting Access Table from a User Form

    I create tables in the database to store project files. I've been testing a method of connecting to these tables (ex. tblTest1) from a Form using Recordset and displaying a single field in a textbox, see below:

    Code:
    Private Sub Form_Open(Cancel As Integer)
    Dim rs As Recordset
    
    Set rs = CurrentDb.OpenRecordset("SELECT * from tblTest1")
    Set Me.Recordset = rs
    Me.txtDiscipline.ControlSource = "Discipline"
    
    End Sub
    ------------------------------
    This works. However, when I try to create an InputBox to allow the user to select the specific table, It doesn't work. Sample code below. What am I doing wrong?
    ---------------------------

    Code:
    Private Sub Form_Open(Cancel As Integer)
    Dim tblFile As String
    Dim rs As Recordset
    
    tblFile = InputBox("Enter the name of the Project File ex. tbl_FileName")
        If Len(tblFile) = 0 Then
            MsgBox ("No Project File name was specified")
          Exit Sub
        End If
    Set rs = CurrentDb.OpenRecordset("SELECT * from tblFile")
    Set Me.Recordset = rs
    Me.txtDiscipline.ControlSource = "Discipline"
    End Sub
    ---------------------
    Last edited by Rabbit; Apr 6 '15, 03:49 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    You've to substitute the tblFile with the entered value like:
    Code:
    Set rs = CurrentDb.OpenRecordset("SELECT * from " & tblFile)

    Comment

    • SteveG2013
      New Member
      • Apr 2015
      • 2

      #3
      Nico

      That was it. Thank you.

      Comment

      • nico5038
        Recognized Expert Specialist
        • Nov 2006
        • 3080

        #4
        Glad I could help.
        Personally I use in cases like this a piece of code to have the user navigate to the folder and after selecting the folder I put the files of this folder in a listbox. Thus the user can only select an existing file and doesn't have to recall the name of the file.

        Nic;o)

        Comment

        Working...