3

私はアクセスが初めてなので、ここで我慢してください。

テーブルに新しいデータを追加できるフォームがあります

ID  | Name |  City  | Zip Code
1   | John | Newark | 12340
2   | Alex | Boston | 98760

などなど…

上記のデータ フィールドを使用して新しいレコードを追加する前に、名前、都市、郵便番号の組み合わせが既に存在するかどうかを確認するテーブルを確認するチェックを作成する必要があります。もしそうなら、Exit Subにしたいです。それ以外の場合は、マクロの残りを続行します。

なんらかの形式の OpenRecordset コマンドを使用してこれを構築しようとしてきましたが、どこから始めればよいかわかりません。誰かが私を正しい方向に向けることができますか? ありがとう!

4

2 に答える 2

1

あなたの状況を再現するためにこのコードを書いたところ、うまくいきました。クエリで列とテーブルの名前を変更するだけです。

Dim strSQL As String
Dim qdf As QueryDef

'if these columns are not text change to approriate type
strSQL = "PARAMETERS [NameToCheck] Text(255),[CityToCheck] Text(255),[Zip] Text(255); " 

'change table name and column names here
strSQL = strSQL & "SELECT Count(*) FROM address " _ 
                & "WHERE FName = [NameToCheck]  AND City = [CityToCheck] AND ZipCode = [Zip];"

Set qdf = CurrentDb.CreateQueryDef("", strSQL)
qdf("NameToCheck") = txtName.Value 'change to that textfield on form
qdf("CityToCheck") = txtCity.Value 'change to that textfield on form
qdf("Zip") = txtZipCode.Value 'change to that textfield on form

If qdf.OpenRecordset(dbOpenSnapshot)(0) > 0 Then
    MsgBox "This record is already in the database"
Else
    'Insert statement goes here.
End If
于 2013-08-05T19:18:38.047 に答える
0

要求どおりにレコードセットを使用する場合は、SQL ステートメントを使用してすべてを選択するか、それを使用して名前で何かを検索する必要があります。

Dim myR as Recordset
Dim strSQL as String

'run a SQL statement to select a record with the same info
strSQL = "SELECT [Name], [City], [Zip Code] FROM table_name_here " & _
         "WHERE [Name] = '" & form_control_name & "' " & _
         "AND [City] = '" & form_control_city & "' " & _
         "AND [Zip Code] = '" & form_control_zip & "'"

'set your recordset to the SQL statment
Set myR = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)

'if your count is greater than 0, then you'll have a duplicate
If myR.RecordCount > 0 then
    MsgBox "This already exists"
Else
    MsgBox "All clear"
End if

Set myR = Nothing
于 2013-08-05T18:08:43.817 に答える