Question : Pass user-defined table name to another form and run DoCmd.CopyObject

Good afternoon -
My question is regarding passing a table name as a variable from one form to another. I know there are few similar threads out there but I am not sure what is missing in my case. Anyway, here is what I am trying to do.

My first form (Import File) imports a txt file to a table via a cmdImport button. The user defines the table name in a text box called 'txttable'. A second form (Recordchecks) is then opened by a cmdProceed button so that I can perform analysis on the table by running SQL queries in VBA. The code for the cmdProceed button identifies the user-defined tablename 'Me.txttable'.

Before running the code for the queries, I want to copy the user-defined table and save it as a static table named 'BillFile'. I will then peform the desired SQL analysis by writing records out of 'BillFile' into new tables.

However, the problem is that my code errors out (error 2498 - wrong data type) on the DoCmd.CopyObject statement in the Recordchecks code. I have an unbound text box on Recordchecks called 'strtable' and it looks like this box is not picking up the name from the DoCmd.OpenForm statement (i.e. 'Me.strtable = Null' )

My current code sample is below. Does anyone know how to correct this error so I can run the DoCmd.CopyObject statement successfully?

Thanks in advance and please let me know if anything is unclear / needs clarification
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
Private Sub cmdImport_Click()
On Error Resume Next
   
   Dim strtable As String
   strFile = Nz(Me.txtFile, "")
   strtable = Nz(Me.txtTable, "")
   
    If strtable = "" Then
        MsgBox "Enter Table Name", vbOKOnly
        End: Exit Sub
    End If
   
   copystrtable = strtable & "_copy"
   strtable = strtable & "_master"
     
   If Right(Dir(strFile), 4) = ".txt" Then
        DoCmd.TransferText acImportFixed, "TCCFORMAT", strtable, strFile, False
        DoCmd.TransferText acImportFixed, "TCCFORMAT", copystrtable, strFile, False
    End If
    
    convert_data (strtable)          
        
End Sub
 
Private Sub cmdProceed_Click()
 
    DoCmd.OpenForm "Recordchecks", , , , , , Me.txtTable
  
End Sub
-----------------------------------------
Private Sub Form_Current()
    
    Me.strtable = Me.OpenArgs
    
End Sub
 
Private Sub Command0_Click()
 
    Dim strSQL As String
    Dim strSQL1 As String
    Dim strSQL2 As String
    Dim MdTable As String
 
    	    'Copy the user-defined table and save as BillFile
            
            DoCmd.CopyObject , "BillFile", acTable, Me.strtable
                    
	  'Move the records from BillFile into a new table for analysis
	
            strSQL = "SELECT BillFile.* INTO [Missing Data]" & _
                     "FROM Billfile;"
    
            'Delete all records from BillFile and keep fields to write new data into the new table based on 
 
query
            
            strSQL1 = "DELETE [Missing Data].*" & _
                      "FROM [Missing Data];"
                      
            'Run desired SQL query etc.
 
            strSQL2 = "INSERT INTO [MissingData] ....
    		      "FROM BillFile "....
            
	    'Date stamp the MissingData table.
            
	    MdTable = Me.strtable & "MissingData"
            DoCmd.CopyObject , MdTable, acTable, "MissingData"
            
    'Run the SQL steps in order
 
    DoCmd.RunSQL strSQL
    DoCmd.RunSQL strSQL1
    DoCmd.RunSQL strSQL2

Answer : Pass user-defined table name to another form and run DoCmd.CopyObject

Good morning, I think I have come across an answer that works in linking the table names

I used:  DoCmd.OpenForm "Recordchecks"

and then:

Private Sub Form_activate()
Me.strtable = Forms![Import File]!txtTable
End Sub

Thanks.
Random Solutions  
 
programming4us programming4us