1

わかりました、Excel 2010 から VBA を使用してアクセスでデータをクエリする際に問題が発生しています。問題は、変数 Descripcheck または Grouplocal の場合に発生します。一部の説明の Excel セルに "" が含まれているため、文字列自体をプルすると、クエリ関数で構文エラーが発生します。何か案は?

            PMnum = Cells(B, 3)
            Grouplocal = Cells(B, 4)
            Descripcheck = Cells(B, 6)
            DevTyp = Cells(B, 5)
            TagName = Cells(B, 2)

            If PMnum = "" Then
            PMnum = " IS NULL"
            Else:
            PMnum = "=" & PMnum
            End If

            If Grouplocal = "" Then
            Grouplocal = " IS NULL"
            Else:
            Grouplocal = "=" & Chr$(34) & Grouplocal & Chr$(34)
            End If

            If Descripcheck = "" Then
            Descripcheck = " IS NULL"
            Else:
            Descripcheck = "=" & Chr$(34) & Descripcheck & Chr$(34)
            End If

            If DevTyp = "" Then
            DevTyp = " IS NULL"
            Else:
            DevTyp = "=" & Chr$(34) & DevTyp & Chr$(34)
            End If

            If TagName = "" Then
            TagName = " IS NULL"
            Else:
            TagName = "=" & Chr$(34) & TagName & Chr$(34)
            End If

            sCmndString = "SELECT Site_Data.Pass_Fail, Site_Data.Tag_Name, Site_Data.[PM_#],Site_Data.Group_Location_Reference, Site_Data.Device_Type, Site_Data.Description, Site_Data.Set_Point, Site_Data.Set_Point_Units, Site_Data.Fail_Low, Site_Data.Fail_High, Site_Data.As_Found, Site_Data.As_Left, Site_Data.Manufacturer_SN, Site_Data.Year_Put_Into_Service, Site_Data.Date_of_Test, Site_Data.Time_To_Complete, Site_Data.Service, Site_Data.Comments, Site_Data.Site, Site_Data.Year, Site_Data.Month " & _
                  "FROM Site_Data WHERE (((Site_Data.[PM_#])" & PMnum & ") AND " & _
                  "((Site_Data.Group_Location_Reference)" & Grouplocal & ") AND " & _
                  "((Site_Data.Device_Type)" & DevTyp & ") AND " & _
                  "((Site_Data.Description)" & Descripcheck & ") AND " & _
                  "((Site_Data.Site)=" & Chr$(34) & SiteName & Chr$(34) & ") AND " & _
                  "((Site_Data.Year)=" & Chr$(34) & yrs & Chr$(34) & ") AND " & _
                  "((Site_Data.Month)=" & Chr$(34) & Mnth & Chr$(34) & ") AND " & _
                  "((Site_Data.Tag_Name)" & TagName & "));"
            Set rs = New ADODB.Recordset
            rs.Open sCmndString, cnt, 2, 3, 1
4

1 に答える 1