2017-01-20 10 views
0

私は最初、テキストファイルの日付を入力してインポートをクリックすることができるテキストボックスを設定したがっています(fromを作成してコーディングしましたが失敗しました。私の問題)。これは、テキストファイルをつかんでテーブルにインポートします。ファイル名を含むテキストファイルをインポートする

これらは週ごとのレポートで、表にインポートする必要があります。 研究と試行錯誤を通じて私の最善の選択肢は、ここでマクロ に出てきたことは私のコードです:

Function InsertCMS_Reports_2ndSave() 
    'DoCmd.DeleteObject Table, "CCS_Reports_2ndSave" 
    DoCmd.TransferText acImportFixed, "CCS_Reports_Import", _ 
    "CCS_Reports_Import", "C:\Users\ABCDEF2\Desktop\January CCS reports for Centene\ABC_COMPRPT_1701011028174_h0062.txt" 
End Function 

それがない私は、データベースを起動したときに、私はマクロを構築し、割り当てられたので、それが自動的にマクロを実行します名前、それへのautoexec。マクロが行うことは、私が作成したインポート仕様に基づいてテキストファイルのデータをテーブルに追加することです。驚いたことに、それは完全にそれをインポートするが、私はそれをやろうとしているより多くのものがあります。おそらく複数のスレッド/質問を作成する必要がありますので、ここでは1つの質問のみを含めます。

1)これらのファイルをインポートする際に、最後の列にファイルの名前を追加し、ファイル名をすべての行に表示するにはどうすればよいですか。

この時点で、開いたマクロは常に無効にするか、ファイル名を新しい週次ファイルに変更して保存し、閉じてから再度開く必要があります。それは最も効率的ではありませんが、うまくいくようです。

+0

フィールドを更新するためにUPDATEクエリを実行するだけでしたが、そのクエリはどこで正確に実行されますか? – KKP

+0

私は更新クエリを使用して終了し、それは私の問題を解決しました。誰かがもっと効率的だと分かっているなら、私に知らせてください。 – KKP

+0

誰でも私がこのマクロを実行してから、いつでもDBを起動する必要はありません。私もそれを選択したときに実行することができますか? VBA内の – KKP

答えて

0

これはあなたが望むだけのことを行うはずです。私はそこにも有益なコメントを入れました。

Private Sub Command1_Click() 

     Dim strPathFile As String, strFile As String, strPath As String 
     Dim strTable As String 
     Dim blnHasFieldNames As Boolean 

     ' Change this next line to True if the first row in EXCEL worksheet 
     ' has field names 
     blnHasFieldNames = False 

     ' Replace C:\Documents\ with the real path to the folder that 
     ' contains the EXCEL files 
     strPath = "C:\Users\Excel\Desktop\test\" 

     ' Replace tablename with the real name of the table into which 
     ' the data are to be imported 
     strTable = "tablename" 

     strFile = Dir(strPath & "*.txt") 
     Do While Len(strFile) > 0 
       strPathFile = strPath & strFile 
       DoCmd.TransferText acImportDelim, , strTable, strPathFile, True 

       On Error Resume Next 
       CurrentDb.Execute "ALTER TABLE tablename ADD FileName CHAR(25)" 

       CurrentDb.Execute "UPDATE tablename SET FileName = '" & _ 
       strFile & "' WHERE FileName IS NULL", dbFailOnError 

     strFile = Dir() 
     Loop 

End Sub 

ローディングについては、あなたはいつファイルを入手しますか?私はファイルが一日に一度来ると思います。それは現実的ですか?以下のチュートリアルに基づいてWindows TaskSchedulerを実行することができます。私があなただった場合

http://www.digitalcitizen.life/how-create-task-basic-task-wizard

最後に、私は一晩中仕事としてこれを行うだろう。

Create an AutoExec macro 

    If you have already created a macro that contains the actions that you want to occur when the database starts, just rename the macro AutoExec, and it will run the next time that you open the database. Otherwise, follow these steps to create a macro: 

     On the Create tab, in the Other group, click Macro. If this command is unavailable, click the arrow beneath either the Module or the Class Module button, and then click Macro. 

     In the Macro Builder, in the first empty Action cell, select the action that you want to perform. If applicable, under Action Arguments, type the appropriate values in 

the argument boxes. 

    If you cannot find the action you want, on the Design tab, in the Show/Hide group, make sure Show All Actions is selected. This expands the list of actions that you can use, but the list will include some actions that will only run if the database is granted trusted status. For more information, see the articles Decide whether to trust a database or How database objects behave when trusted and untrusted. 

    Repeat step 2 for each additional action you want to occur. 

    Click Save, and in the Save As dialog box, type AutoExec. 

    Click OK and then close the Macro Builder. The new macro will run the next time that you open the database. 
関連する問題