テキストファイルを使用して、アクセスフォームのフィールドのデフォルト値を入力します。

StackOverflow https://stackoverflow.com//questions/23065910

質問

マイクロソフトのアクセスウィンドウは、ユーザーが入力する必要があるテキストボックスフィールドを含むポップアップウィンドウを持っています。

First Name:
Last Name:
.

クリックしたときにC:\ MyTextFile.txtを調べるボタンを作成しようとしています それらのフィールドを自動入力します。

テキストファイル内部このようになります:

##$@#%#$543%#$%#$$#%LAST NAME:BOB#$#@$@#$@#$@#FIRST NAME:DERRICK$#%$#%$#%#$%$#%$#
.

本質的に私は3つのことを探しています:

  1. テキストファイルにアクセスする
  2. データを解析する
  3. テキストボックスに入るために。(「保存」ボタンがクリックされるまでデータを入れる必要はありません」)
  4. 更新: これは私がこれまでに書かれたものです、私はそれが機能していないのかわからない。

    Private Sub LoadText_Click()
    
        Dim myFile As String myFile = "C:\myFile.txt"
        Me.NameofTextbox = Mid(myFile, 7, 3)
    
    End Sub
    
    .

役に立ちましたか?

解決

ここでは、txtboxLastNametxtboxFirstName

という名前のフォームに提供したファイルの例
Dim mFields() As String ' array with fields' names in file
Dim mControls() As String ' corresponding controls' names
Dim mStopChars() As String ' Characters that put after values

Dim tmpstr As String
Dim content As String

Dim i As Long
Dim fStart  As Long
Dim valStart As Long
Dim valEnd As Long
Dim FieldValue As String
Dim j As Long
Dim tmp As Long

' prepare maps

' here : included in field name for common case
mFields = Split("LAST NAME:,FIRST NAME:", ",") 
mControls = Split("txtboxLastName,txtboxFirstName", ",")
mStopChars = Split("#,$,@,%", ",")

' read file into string
Open "c:\mytextfile.txt" For Input As #1

Do While Not EOF(1)
    Input #1, tmpstr
    content = content & tmpstr
Loop

Close #1

' cycle through fields and put their values into controls
For i = LBound(mFields) To UBound(mFields)
    fStart = InStr(1, content, mFields(i))
    If fStart > 0 Then
        valStart = fStart + Len(mFields(i))  'value start at this pos
        'cycle through possible stop chars to locate end of current value
        valEnd = Len(content)
        For j = LBound(mStopChars) To UBound(mStopChars)
            tmp = InStr(valStart, content, mStopChars(j))
            If tmp > 0 Then
                If tmp <= valEnd Then
                    valEnd = tmp - 1
                End If
            End If
        Next j
        ' cut value
        FieldValue = Mid(content, valStart, valEnd - valStart + 1)
        ' assign to control
        Me.Controls(mControls(i)).Value = FieldValue
    End If
Next i
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top