473,549 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ 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 1142
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(d Set);

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

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

Similar topics

6
2046
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(); if (isset($HTTP_SESSION_VARS))
22
1457
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
1669
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 Page 1</title> </head>
10
1545
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
3436
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 current web request. Please review the stack trace for more information about the error and where it originated in the code.
3
5935
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 AxWebBrowser1.Document.Forms(0).All.item("action-download").click() "action-download" is the name of a submit button
13
1327
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>: You must enable javascript for this page to work properly!</span>') </noscript>
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
2721
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() { int MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 30000; string Script = @" <script type='text/javascript'> var Count=0;
7
6198
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, outputs "null". The third alert() shows a particular elements textContent. Atleast in Firefox. In Internet Explorer it returns "undefined". ...
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6048
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
766
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.