473,486 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why doesn't this sql statement work

Tig201
103 New Member
I am having difficulties with the following code. When I run it I get a zero length table but if I copy the sql statement out of the third text box go into access and paste it into the sql view and run it I get the one record I’m looking for.

Expand|Select|Wrap|Line Numbers
  1.         Dim CaddTxtFile As String = "C:\WINDOWS\Temp\Acad_Log_Out.txt"
  2.         Dim CaddFile As String, SqlTxt As String
  3.  
  4.         Dim dSet As New DataSet
  5.         Dim dAdapter As OleDb.OleDbDataAdapter
  6.  
  7.         Dim TxtStream As New System.IO.StreamReader(CaddTxtFile)
  8.         CaddFile = TxtStream.ReadLine
  9.         TxtStream.Close()
  10.  
  11.         CaddFile = CaddFile.Substring(0, 5) & "*"
  12.         SqlTxt = "SELECT CADD_File_Name, Drawing_Number, Drawing_Title FROM Drawing_Log" & " WHERE CADD_File_Name LIKE '" & CaddFile & "'"
  13.  
  14.         dbCon.Open()
  15.         dAdapter = New OleDb.OleDbDataAdapter(SqlTxt, dbCon)
  16.         dAdapter.Fill(dSet, "Log")
  17.  
  18.         dbCon.Close()
  19.  
  20.         TxtCadd.Text = dSet.Tables("log").Rows.Count
  21.         TxtDwg.Text = CaddFile
  22.         TxtTitle.Text = SqlTxt
  23.  
Any help would be appreciated.
Sep 3 '10 #1
10 1140
Plater
7,872 Recognized Expert Expert
Well I've never seen an unnamed table query return a table name. Get rid of the , "Log") section and use
dAdapter.Fill(dSet);

Note: You can also fill just a DataTable instead of a DataSet if you only are returning one table.
Sep 3 '10 #2
Tig201
103 New Member
If I change that then what do I change line 20 to to get the information back out of the dataset. Besides My code works fine if i comment out
Expand|Select|Wrap|Line Numbers
  1. & " WHERE CADD_File_Name LIKE '" & CaddFile & "'"
  2.  
Sep 3 '10 #3
Jerry Winston
145 Recognized Expert New Member
Your ad hoc sql
Expand|Select|Wrap|Line Numbers
  1. "SELECT CADD_File_Name, Drawing_Number, Drawing_Title FROM Drawing_Log" & " WHERE CADD_File_Name LIKE '" & CaddFile & "'"
Evaluates to:
Expand|Select|Wrap|Line Numbers
  1. SELECT CADD_File_Name, Drawing_Number, Drawing_Title FROM Drawing_Log WHERE CADD_File_Name LIKE 'someFileName'
The problem is your like statement is missing the underscore or wildcard character '%'. I think you want your code to look like this:
Expand|Select|Wrap|Line Numbers
  1. "SELECT CADD_File_Name, Drawing_Number, Drawing_Title FROM Drawing_Log" & " WHERE CADD_File_Name LIKE '%" & CaddFile & "%'"
or
Expand|Select|Wrap|Line Numbers
  1. "SELECT CADD_File_Name, Drawing_Number, Drawing_Title FROM Drawing_Log" & " WHERE CADD_File_Name LIKE '_" & CaddFile & "%'"
Sep 3 '10 #4
Tig201
103 New Member
Here is the Sql String that is compiled by the code.
Expand|Select|Wrap|Line Numbers
  1. SELECT CADD_File_Name,Drawing_Number,Drawing_Title
  2. FROM Drawing_Log
  3. WHERE CADD_File_Name LIKE 'C1130*'
  4.  
I would have prefered 'C1130*' to be "C1130*"
But am unsure how to do this and the sql works in access with the single quote vice the double quote.
Sep 3 '10 #5
Jerry Winston
145 Recognized Expert New Member
What's your database? SQL or Access? What versions?
Sep 3 '10 #6
Tig201
103 New Member
My database is Access 2003
Sep 3 '10 #7
Jerry Winston
145 Recognized Expert New Member
It looks like you're coding in VB.NET. If you are use two " marks to insert a single " inside of quotes.
Expand|Select|Wrap|Line Numbers
  1. "SELECT CADD_File_Name,Drawing_Number,Drawing_Title
  2. FROM Drawing_Log
  3. WHERE CADD_File_Name LIKE ""C1130*""  "
  4.  
Also, add a break point at the top line and inspect your data objects while your code is running.
Sep 7 '10 #8
Tig201
103 New Member
I've been playing with this and I think I've narrowed it down to the wild card character. Here is my Code
Expand|Select|Wrap|Line Numbers
  1. Dim CaddTxtFile As String = "C:\WINDOWS\Temp\Acad_Log_Out.txt"
  2. Dim CaddFile As String, SqlTxt As String
  3.  
  4. Dim dSet As New DataSet
  5. Dim dAdapter As OleDb.OleDbDataAdapter
  6.  
  7. Dim TxtStream As New System.IO.StreamReader(CaddTxtFile)
  8. CaddFile = TxtStream.ReadLine
  9. TxtStream.Close()
  10.  
  11. CaddFile = ControlChars.Quote & CaddFile.Substring(0, 5) & "*" & ControlChars.Quote
  12. CaddFile = ControlChars.Quote & CaddFile.Substring(0, 5) & "-XX" & ControlChars.Quote
  13. SqlTxt = "SELECT CADD_File_Name, Drawing_Number, Drawing_Title FROM Drawing_Log" & " WHERE CADD_File_Name LIKE " & CaddFile
  14.  
  15. dbCon.Open()
  16. dAdapter = New OleDb.OleDbDataAdapter(SqlTxt, dbCon)
  17. dAdapter.Fill(dSet, "Log")
  18. dbCon.Close()
  19.  
  20. TxtCadd.Text = dSet.Tables("Log").Rows.Count
  21. TxtDwg.Text = CaddFile
  22. TxtTitle.Text = sqlTxt'dSet.Tables("Log").Rows(0).Item(2)
  23.  
Line 11 returns 0 rows where Line 12 returns 1 row. Since the database I’m using has multiple variations for the last three characters I need to be able to use a wild card. Can anybody explain how to use a wild card in this situation? And before somebody suggest it have already tried
Expand|Select|Wrap|Line Numbers
  1. CaddFile = ControlChars.Quote & CaddFile.Substring(0, 5) & "???" & ControlChars.Quote
  2.  
to no avail.
Sep 7 '10 #9
Plater
7,872 Recognized Expert Expert
Try using the % as the wildcard anyway? The .NET warraper classes might convert it to a *, but use the % for consistency
Sep 7 '10 #10
Tig201
103 New Member
Thank you Plater that was the answer. Sorry Jerry you gave me the answer earlier and I missed it. I didn't realize the % replaced the *. Thanks everybody for all your help.
Sep 7 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2043
by: NotGiven | last post by:
session_start(); if (isset($HTTP_SESSION_VARS))&&($HTTP_SESSION_VARS != '') echo "hello"; It doesn't throw an error it just doesn't display anything However, this works: session_start();...
22
1455
by: Chris Schumacher | last post by:
I wrote the following program to demonstrate an oddity I found in C++. #include <iostream> using namespace std; int main() {int *p, *q; p = q; q = new int;
3
1658
by: Iver Erling Årva | last post by:
Can anyone please tell me why this doesn't work? The sign changes when I hit the button, and I get no error messages, but the textarea doesn't disappear/reappear. <html> <head> <title>New...
10
1541
by: Brett | last post by:
This code is supposed to work in Netscape 4+ and IE 4+. It works fine in IE but in Netscape 7.2, I get a blank page. Any suggestions? Thanks, Brett <html> <head>
3
3432
by: Brian Henry | last post by:
I get this error Server Error in '/' Application. IErrorInfo.GetDescription failed with E_FAIL(0x80004005). Description: An unhandled exception occurred during the execution of the...
3
5924
by: MeNotHome | last post by:
I am trying to automate web browser navigation and form fill out in vb.net Why doesn't this work? AxWebBrowser1.Document.Forms(0).All("action-download").click() I also tried...
13
1317
by: Giggle Girl | last post by:
My pages need to unfold gracefully even if Javascript is disabled, but I can't get this to work? Please help, Javascript Gurus!! <noscript> document.write('<span class=warning><b>Warning</b>:...
3
1237
by: rynato | last post by:
I have a function moveDiv(divID). to test to see if the div has moved far enough: if (new_left < 400) { the_timeout = setTimeout("moveDiv("myDiv");",10); } WORKS but this DOESN'T
3
2713
by: Joey | last post by:
I am working on an asp.net 1.1 web app in C#. I downloaded some sample code that is supposed to allow for me to persist session state. The code is as follows: private void PersistSessionState()...
7
6187
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/xml.html The first alert() shows the XML that the server is returning. The second alert() shows a particular elements nodeValue and, as you can see,...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.