|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
WHERE clause in ADO query of Excel dataOn Error GoTo ErrorHandler ' Enable error-handling routine. Dim objRsExcel As Object Set objRsExcel = CreateObject("ADODB.Recordset") Dim sConn As String Dim RecCount As Long Dim strExcelPath As String Dim strExcelFileName As String Dim strSheetName As String Dim sUser As String Dim sPW As String Dim bLicAgree As Boolean Dim rNextUser As Range ''CHECK FOR MISSING DATA With Me.txtUserName If Len(.Text) < 4 Then .SetFocus SendKeys "{Home}+{End}" MsgBox "UserNames must be at least 4 characters long.", vbOKOnly, "INVALID OR MISSING ENTRY" Exit Sub End If End With With Me.txtPW If Len(.Text) < 4 Then .SetFocus SendKeys "{Home}+{End}" MsgBox "Passwords must be at least 4 characters long.", vbOKOnly, "INVALID OR MISSING ENTRY" Exit Sub End If End With ''IDENTIFY THE DATA SOURCE strExcelPath = App.Path & "\" strExcelFileName = "Test.xls" strSheetName = "Codes" ''QUERY PARAMETERS objRsExcel.CursorLocation = 1 objRsExcel.CursorType = 1 objRsExcel.LockType = 1 ''CONNECTIONS STRING sConn = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ=" & strExcelPath & strExcelFileName '''SQL STRING ''******************************* '\\ I want to change the following line to include a WHERE CLAUSE '\\ See it at the bottom. objRsExcel.Open "SELECT * FROM [" & strSheetName & "$]", sConn '\\****************************************************** ''LOOP THRU THE RECORD SET Do Until objRsExcel.EOF sUser = IIf(Not IsNull(objRsExcel.fields("A").value), objRsExcel.fields("A").value, "Null") sPW = IIf(Not IsNull(objRsExcel.fields("B").value), objRsExcel.fields("B").value, "Null") bLicAgree = IIf(objRsExcel.fields("C") = "TRUE", True, False) ''SHOW THE RESULTS OF THE QUERY MsgBox "User Name : " & sUser & vbCr & "Password : " & sPW & vbCr & "Licensed : " & bLicAgree, vbOKOnly + vbInformation, App.Title objRsExcel.MoveNext Loop ''CLOSE objRsExcel.Close Set objRsExcel = Nothing Unload Me Exit Sub ErrorHandler: ' Error-handling routine. '//Create Error Log File in Application Path Open App.Path & "\" & "ImportExcelFile.log" For Append As #1 Write #1, "Error Number : " & Err.Number, "Error Description : " & Err.Description, Now ' Write comma-delimited data. Close #1 ' Close before reopening in another mode. '// Up to This Err.Clear End End Sub '\\ I'D LIKE TO INCLUDE A WHERE CLAUSE IN THE SQL STATEMENT SIMILAR '\\ TO THE FOLLOWING. HOW SHOULD I CHANGE THIS TO MAKE IT WORK? ' objRsExcel.Open "SELECT * FROM [" & strSheetName & "$]" & "WHERE " & objRsExcel.fields("B").value = sPW, sConn Hi Cush,
You're pretty much there with your initial try... objRsExcel.Open "SELECT * FROM [" & strSheetName & "$]" & "WHERE B ='" & sPW & "'", sConn but looking at your code, you haven't initialised sPW before you use it in the SQL string. Without diving into your code too much, you may be better off using the .Filter property of the connection object. i.e. objRsExcel.Open "SELECT * FROM [" & strSheetName & "$]" .. .. .. .. ' set sPW to some value and then objRsExcel.Filter = "B ='" & sPW & "'" HTH Martin Show quoteHide quote "cush" <c***@discussions.microsoft.com> wrote in message news:FEF5B06C-A891-43B1-8E65-12FC97E9C119@microsoft.com... > Private Sub btnOK_Click() > > On Error GoTo ErrorHandler ' Enable error-handling routine. > > Dim objRsExcel As Object > Set objRsExcel = CreateObject("ADODB.Recordset") > Dim sConn As String > Dim RecCount As Long > > Dim strExcelPath As String > Dim strExcelFileName As String > Dim strSheetName As String > Dim sUser As String > Dim sPW As String > Dim bLicAgree As Boolean > > Dim rNextUser As Range > > ''CHECK FOR MISSING DATA > With Me.txtUserName > If Len(.Text) < 4 Then > .SetFocus > SendKeys "{Home}+{End}" > MsgBox "UserNames must be at least 4 characters long.", > vbOKOnly, "INVALID OR MISSING ENTRY" > Exit Sub > End If > End With > > With Me.txtPW > If Len(.Text) < 4 Then > .SetFocus > SendKeys "{Home}+{End}" > MsgBox "Passwords must be at least 4 characters long.", > vbOKOnly, "INVALID OR MISSING ENTRY" > Exit Sub > End If > End With > > ''IDENTIFY THE DATA SOURCE > strExcelPath = App.Path & "\" > strExcelFileName = "Test.xls" > strSheetName = "Codes" > > ''QUERY PARAMETERS > objRsExcel.CursorLocation = 1 > objRsExcel.CursorType = 1 > objRsExcel.LockType = 1 > > ''CONNECTIONS STRING > sConn = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ=" & > strExcelPath > & strExcelFileName > > '''SQL STRING > ''******************************* > '\\ I want to change the following line to include a WHERE CLAUSE > '\\ See it at the bottom. > objRsExcel.Open "SELECT * FROM [" & strSheetName & "$]", sConn > '\\****************************************************** > > > ''LOOP THRU THE RECORD SET > Do Until objRsExcel.EOF > sUser = IIf(Not IsNull(objRsExcel.fields("A").value), > objRsExcel.fields("A").value, "Null") > sPW = IIf(Not IsNull(objRsExcel.fields("B").value), > objRsExcel.fields("B").value, "Null") > bLicAgree = IIf(objRsExcel.fields("C") = "TRUE", True, False) > > ''SHOW THE RESULTS OF THE QUERY > MsgBox "User Name : " & sUser & vbCr & "Password : " & sPW & vbCr & > "Licensed : " & bLicAgree, vbOKOnly + vbInformation, App.Title > objRsExcel.MoveNext > Loop > > ''CLOSE > objRsExcel.Close > Set objRsExcel = Nothing > Unload Me > Exit Sub > > ErrorHandler: ' Error-handling routine. > '//Create Error Log File in Application Path > Open App.Path & "\" & "ImportExcelFile.log" For Append As #1 > Write #1, "Error Number : " & Err.Number, "Error Description : " & > Err.Description, Now ' Write comma-delimited data. > Close #1 ' Close before reopening in another mode. > '// Up to This > Err.Clear > End > End Sub > > > > '\\ I'D LIKE TO INCLUDE A WHERE CLAUSE IN THE SQL STATEMENT SIMILAR > '\\ TO THE FOLLOWING. HOW SHOULD I CHANGE THIS TO MAKE IT WORK? > > ' objRsExcel.Open "SELECT * FROM [" & strSheetName & "$]" & "WHERE " & > objRsExcel.fields("B").value = sPW, sConn >
MS, Enough is Enough!
Can someone please check my code? No Intellisense for items About The Hard Disk Serial Number VB and Excel Crazy VB Convert Variant String to Double Creating an ActiveX control Acces Denied when calling FaxDocument Submit What considerations must be taken for app to run as a service? Lock Word window? |
|||||||||||||||||||||||