Click here to Skip to main content
15,921,530 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone I have the following query, this query works fine in the query builder in MS Access:
VB
UPDATE PAYTEST SET PAYTEST.ImageName = "C:\Ezc\Files\" & [bates] & " *.TIF";


Howerver when I try to execute in from vb.net, It does not work.

Here is my vb.net code:

VB
Public Shared Function UpdateImagePathSQL(ByVal newPath As String) As Integer
       Dim cn As OleDbConnection
       Dim intNumberOfRecordsUpdated As Integer
       Try
           cn = New OleDbConnection
           cn.Open()
           Dim selectStatement As String
           selectStatement = _
                "UPDATE Paytest SET Imagename = '" & newPath & "'" & "\[bates].TIF"

           Dim cmd = New OleDbCommand(selectStatement, cn)
           intNumberOfRecordsUpdated = cmd.ExecuteNonQuery()
           cn.Close()
           MessageBox.Show("Records updated sucessfully", "Timesheet imagepath update")
           Return intNumberOfRecordsUpdated
       Catch ex As Exception
           MessageBox.Show(ex.Message, "Error updating image path")
           cn.Close()
       End Try


[bates] is a field in my access table that contains the image name without the extension.

What Am I doing wrong?

Thanks

Ed
Posted
Comments
bbirajdar 11-Feb-13 0:07am    
I can see that you have not set up the connection string..The path to access database

Hi,
I think, you problem with the update query, check the below.

VB
selectStatement = _
                 "UPDATE Paytest SET Imagename = '" & newPath & "\[bates].TIF'"


Best Regards
Muthuraja
 
Share this answer
 
Comments
bbirajdar 10-Feb-13 11:47am    
Nice catch .Missing the apostrophe.... +5.. And that too for a vague question which just says -'does not work'...
elchalateco 10-Feb-13 13:34pm    
I am sorry if the question is 'vague' however the solution provided did not work. [bates] is not a variable is the name of a field in the access table.

Thanks
bbirajdar 11-Feb-13 0:06am    
Same again...You need to provide the exact error message so that we can help you. 'Does not work' does not help us in anyway to understand your problem...
elchalateco 11-Feb-13 10:06am    
Thanks for the input. I will try to be more specific when posting new questions.
Hi,

This query works fine for you in the query builder in MS Access:
SQL
UPDATE PAYTEST SET PAYTEST.ImageName = "C:\Ezc\Files\" & [bates] & " *.TIF";

And this is selectStatement:
VB
Dim selectStatement As String
           selectStatement = _
                "UPDATE Paytest SET Imagename = '" & newPath & "'" & "\[bates].TIF"

In selectStatement, change Imagename into PAYTEST.Imagename, and Paytest into PAYTEST.
Also, you need to put the second apostrophe AFTER the file path:
VB
Dim selectStatement As String
            selectStatement = _
                 "UPDATE PAYTEST SET PAYTEST.Imagename = '" & newPath & "\[bates].TIF'"

Hope this helps.
 
Share this answer
 
v4
Comments
elchalateco 10-Feb-13 12:12pm    
Hi ProgramFox,
Unfortunatelly it did not work, I tryed to re-post the question however I realized that I posted a solution. Please see what I posted.
Thomas Daniels 10-Feb-13 12:14pm    
I tryed to re-post the questin however I realized that I posted a solution
You can improve your question using the "Improve question button".
I will update my answer.
Thomas Daniels 10-Feb-13 12:15pm    
I updated my answer.
elchalateco 10-Feb-13 13:05pm    
Hi ProgramFOX.
Still No luck.

When I i run the query I get "C:\images\[bates].tif" in the imagename field when I should see:
"C:\images\img0001.tif".
'img0001' is the value stored in the bates field in the access table.

And obviously this value is different for every record in the table
Thomas Daniels 10-Feb-13 13:06pm    
Change [bates] into the name of your image.
I tryed both sugestion and unfortunatelly none of them work.
I get an exception:

VB
System.Data.OleDb.OleDbException was caught
  ErrorCode=-2147217904
  Message="No value given for one or more required parameters."
  Source="Microsoft JET Database Engine"


For example if bates contains "IMG0002" and the value of new path is "C:\ImgPath\" The final result of ImageName should be "C:\ImgPath\IMG0002.TIF" however when I run the query the result is "C:\ImgPath\[bates].TIF" which is wrong because [bates] is a field in my MS Access table.

Any other sugestions?

Thanks very much.

Ed
 
Share this answer
 
Step through the program in your debugger and stop it immediately after the assignment to selectStatement. Display selectStatement in the watch window or Immediate window.

You want it to look exactly like this ...
UPDATE Paytest SET Imagename = 'C:\Ezc\Files\' + [bates] + '.TIF'


Which I expected ProgramFOXs statement suggestion would have achieved ...

VB
selectStatement = "UPDATE Paytest SET Imagename = '" + newPath + "' + [bates] + '.TIF'"


NB this is assuming newPath = @"C:\Ezc\Files\"
 
Share this answer
 
v2
Comments
elchalateco 11-Feb-13 10:31am    
Hi Clill60,
Even though I workedout a solution, it doesn't seem fast to me.

When I use: UPDATE Paytest SET Imagename = 'C:\ezc\files\' + [bates] + '.TIF'
This line won't compile at all, VB complains with the following error:
Name [bates] is not declared.

[bates] is a field in my access database, however VB does not know that, I believe VB thinks this should be a variable declared somewhere.

This query when run from MS access query builder works fine:
UPDATE PAYTEST SET PAYTEST.ImageName = "C:\Ezc\Files\" & [Bates] & ".TIF";
Access some how knows that [bates] is a field within the database.

I need the user to be able to select a path where the images are going to reside in the user's hard drive and perform the update from vg program.

I apreciate your help.

Thanks

Ed.
CHill60 11-Feb-13 10:51am    
In the VB code I supplied [bates] would be enclosed within the double quotes so VB does not throw an error. "UPDATE Paytest SET Imagename = 'C:\Ezc\Files\' + [bates] + '.TIF'" is the contents of the variable selectStatement, not what you type into VB.
I run the update query stored in msaccess from vb.net
Using the following fucntion.
VB
Public Shared Function RunAccessQuery() As Int32
        Dim intRecords As Int32
        Dim con As OleDbConnection
        Dim cmd As OleDbCommand
        Try
            con = New OleDbConnection(TimesheetsDB.GetConnection)
            cmd = New OleDbCommand("quImagePath", con)
            cmd.CommandType = CommandType.StoredProcedure
            con.Open()
            intRecords = cmd.ExecuteNonQuery()
            con.Close()
            Return intRecords
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error running access query")
        End Try
    End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900