dポイントプレゼントキャンペーン実施中!

実行エラー 3075:クエリ式`作成日 の間#2010/10/01# AND #2010/10/30#の構文エラー:演算子というエラーの処理をどうすればいいか分かりませんので教えてほしいです。
Private Sub cmdFilter_Click()
Dim strSQL As String
Dim strCriteria As String

With Me
If Len(.cboFieldName) <> 0 Then
If Len(.cboCondition) <> 0 Then
strCriteria = BuildCriteria(.cboFieldName.Column(0), _
.cboFieldName.Column(1), .cboCondition, _
Nz(.txtValue1), Nz(.txtValue2))
End If
End If
End With
With Me
ここはエラー→ .Filter = strCriteria
.FilterOn = True
.Requery
End With

Private Function BuildCriteria(strFieldName As String, _
intType As Integer, _
strCondition As String, _
varValue1 As Variant, _
Optional varValue2 As Variant) As String

Dim fBetween As Boolean
Dim fLike As Boolean
Dim strCriteria As String
Const conQuotes = """"

fBetween = IIf(InStr(strCondition, "の間") > 0, _
True, False)
fLike = IIf(InStr(strCondition, "類似") > 0, _
True, False)

strCriteria = strFieldName & " "
Select Case intType
Case dbText, dbMemo
If InStr(1, varValue1, "*") > 0 Then
strCriteria = strCriteria _
& " Like " & conQuotes & varValue1 & conQuotes
Else
If fLike Then
strCriteria = strCriteria & " 類似 " _
& conQuotes & "*" & varValue1 & "*" & conQuotes
Else
strCriteria = strCriteria & strCondition _
& " " & conQuotes & varValue1 & conQuotes
End If
End If

Case dbInteger, dbLong, dbCurrency, dbDouble, dbSingle
If fBetween Then
strCriteria = strCriteria _
& strCondition & " " & varValue1 & " AND " & varValue2
Else
strCriteria = strCriteria _
& strCondition & " " & varValue1
End If
Case dbDate
If fBetween Then
strCriteria = strCriteria & strCondition _
& " #" & Format(varValue1, "yyyy/mm/dd") & "# AND #" _
& Format(varValue2, "yyyy/mm/dd") & "#"
Else
strCriteria

A 回答 (4件)

#1、#2です。



cboConditionに元々はLIKE、BETWEENというキーワードが入っていたが、
それを「類似」と「の間」に変更した、ということでしょうか。
本来はキーワードのBETWEENをstrConditionに渡すということであったならば

>Case dbDate
>If fBetween Then
>strCriteria = strCriteria & strCondition _
>& " #" & Format(varValue1, "yyyy/mm/dd") & "# AND #" _
>& Format(varValue2, "yyyy/mm/dd") & "#"

において、

>strCriteria = strCriteria & strCondition _

のところを、

strCriteria = strCriteria & " Between"_

でいいのでは、と思います。"" の中のBetweenの
前は半角空けておきます。
これで、#1で述べましたようにMsgBoxでstrCriteria
の中身が、

作成日 Between #2010/10/01# AND #2010/10/30#

となっていればいいのではと思います。


また、他の

>Case dbInteger, dbLong, dbCurrency, dbDouble, dbSingl

の場合も同様だと思いますが。
    • good
    • 0
この回答へのお礼

お陰様でエラー解決できました。
ありがとうございます。

お礼日時:2010/11/10 09:15

FUNCTION内で定義している


「strCondition」にどんな値が入るのか教えてほしいです。
関係しているかもしれません。

>If fLike Then
>strCriteria = strCriteria & " 類似 " _

「類似」には使われていませんが
他では使われています。
>strCriteria = strCriteria & strCondition _


おかしいときは、
途中にブレイクポイントをおいてデバッグするとか
メッセージボックスを追記して値がどうなっているか見る。
こういうことをしないと自力で解決できなくなりますから、
なぜおかしいか以前にやり方を聞く方がいいですね。
    • good
    • 0

エラーの、



の間#2010/10/01# AND #2010/10/30#

は、本来

Between #2010/10/01# AND #2010/10/30#

になるのが、Between の代わりに 「の間」 が
演算子として入ってしまっているからではと思いますが。

If fBetween Then
strCriteria = strCriteria & strCondition _
& " #" & Format(varValue1, "yyyy/mm/dd") & "# AND #" _
& Format(varValue2, "yyyy/mm/dd") & "#"

のところで、Betweenが入るのは、strConditionの
ところですか。あるいは、
strCriteria = strCriteria & strCondition & "Between"_
とするのですか。
    • good
    • 0
この回答へのお礼

ありがとうございます。
これでエラーの原因だと思います。エラーが解決できました。

お礼日時:2010/11/10 09:19

コードが途中掲載なので、思いついたことだけ。



(1)
With Me
ここはエラー→ .Filter = strCriteria
.FilterOn = True
.Requery
End With

の部分をコメントアウトして、変わりに

MsgBox strCriteria

をいれて、strCriteria の内容を確認
してみては。

(2)
#" & Format(varValue1, "yyyy/mm/dd") & "#
などを、
'" & Format(varValue1, "yyyy/mm/dd") & "'
にしてみるとか。

(3)
あるいは、txtValue1とtxtValue2の値の
対象となるレコードのフィールドなりが
テキスト型ならば、型を変更する。
あるいは、(2)をCaDate、Formatで
変更する。

(4)
BuildCriteriaの他の引数にNullがないか。

この回答への補足

すみません、これは全部のコードです。元々は行けているんですが条件のリストボックスの中にBETWEENとLIKEという条件があって、私は「の間」と「類似」に変わりました。そこでこのエラーに出てしまったんです。類似という条件でうまく検索できますが「の間」だけではいけてないです
txtValue1=ChangeProperty("cmdFilter")
Private Function BuildCriteria(strFieldName As String, _
intType As Integer, _
strCondition As String, _
varValue1 As Variant, _
Optional varValue2 As Variant) As String

Dim fBetween As Boolean
Dim fLike As Boolean
Dim strCriteria As String
Const conQuotes = """"

fBetween = IIf(InStr(strCondition, "の間") > 0, _
True, False)
fLike = IIf(InStr(strCondition, "類似") > 0, _
True, False)

strCriteria = strFieldName & " "
Select Case intType
Case dbText, dbMemo
If InStr(1, varValue1, "*") > 0 Then
strCriteria = strCriteria _
& " Like " & conQuotes & varValue1 & conQuotes
Else
If fLike Then
strCriteria = strCriteria & " Like " _
& conQuotes & "*" & varValue1 & "*" & conQuotes
Else
strCriteria = strCriteria & strCondition _
& " " & conQuotes & varValue1 & conQuotes
End If
End If

Case dbInteger, dbLong, dbCurrency, dbDouble, dbSingle
If fBetween Then
strCriteria = strCriteria _
& strCondition & " " & varValue1 & " AND " & varValue2
Else
strCriteria = strCriteria _
& strCondition & " " & varValue1
End If
Case dbDate
If fBetween Then
strCriteria = strCriteria & strCondition _
& " #" & Format(varValue1, "yyyy/mm/dd") & "# AND #" _
& Format(varValue2, "yyyy/mm/dd") & "#"
Else
strCriteria = strCriteria _
& strCondition & " #" & Format(varValue1, "yyyy/mm/dd") & "#"
End If
Case Else
strCriteria = strCriteria _
& strCondition & " " & varValue1
End Select
BuildCriteria = strCriteria

End Function

補足日時:2010/11/08 16:11
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

関連するカテゴリからQ&Aを探す