Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Run Stored Update Query using VBA (from Word)

Posted on 2010-08-16
5
Medium Priority
?
681 Views
Last Modified: 2012-05-10
Hi All

Please can someone tell me how to run a stored update query in an Access database from Word/VBA?  I have got so far but I am clearly missing something.  By the way, the query works correctly if I run it manually from Access.

Thank you for your help as ever.




Public Sub UpdateDetails()
Dim dbMAIN As New ADODB.Connection
Dim qQuery As New ADODB.Command

With dbMAIN
    .ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" & _
                        "Persist Security Info = False;" & _
                        "Data Source = "C:\Source\Numbering.mdb"
    .Open
End With

With qQuery
    .ActiveConnection = dbMAIN
    .CommandType = adCmdStoredProc
    .CommandText = "qUpdateNumber"
    .Parameters.Append .CreateParameter("NewNumber", adChar, adParamInput, Len("MyNewNumber"), "MyNewNumber")
    .Parameters.Append .CreateParameter("OldNumber", adChar, adParamInput, Len("MyOldNumber"), "MyOldNumber")
    .Parameters.Append .CreateParameter("RecordDate", adChar, adParamInput, Len("MyNewDate"), "MyNewDate")
End With

qQuery.Execute

Set qQuery = Nothing
Set dbMAIN = Nothing
End Sub

Open in new window

0
Comment
Question by:MonteDelMar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 33446085
Hi
What's the definition of qUpdateNumber?
Are you getting any specific error messages?
The one thing I notice right away is that it's astronomically likely that your parameters are operating upon fields of "Text" type in Access. Which is actually a Unicode VarChar.
So
    .Parameters.Append .CreateParameter("NewNumber", adChar, adParamInput, Len("MyNewNumber"), "MyNewNumber")
would be
    .Parameters.Append .CreateParameter("NewNumber", adWVarChar, adParamInput, Len("MyNewNumber"), "MyNewNumber")
and so forth...
Cheers.
0
 

Author Comment

by:MonteDelMar
ID: 33446276
Thank you for your response.

I have re-attached the code here to save confusion.  I had renamed some of the fields in the example I sent you as I (mistakenly) thought it would be easier.  

Do you mean the SQL for the Query?  That is

UPDATE ClientNumbers SET ClientNumbers.ArtClientNumber = [NewNumber], ClientNumbers.RenumberDate = [NewDate]
WHERE (((ClientNumbers.ArtClientNumber)=[OldNumber]));

I do not get an error message.  It appears to run but does not update the record.

I just tried changing the Type to adVarWChar but that has not made a difference.
Public Sub UpdateRecord()
Dim dbMAIN As New ADODB.Connection
Dim qQuery As New ADODB.Command

With dbMAIN
    .ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" & _
                        "Persist Security Info = False;" & _
                        "Data Source =" & strDrive & strLocation & "Numbering.mdb"
    .Open
End With

With qQuery
    .ActiveConnection = dbMAIN
    .CommandType = adCmdStoredProc
    .CommandText = "UpdateClientDetails"
    .Parameters.Append .CreateParameter("NewNumber", adWChar, adParamInput, Len("MyNewClientNumber"), "MyNewClientNumber")
    .Parameters.Append .CreateParameter("OldNumber", adVarWChar, adParamInput, Len("BWC00002"), "BWC00002")
    .Parameters.Append .CreateParameter("RenumberDate", adVarWChar, adParamInput, Len("MyNewDate"), "MyNewDate")
End With

qQuery.Execute

Set qQuery = Nothing
Set dbMAIN = Nothing
End Sub

Open in new window

0
 
LVL 44

Accepted Solution

by:
Leigh Purvis earned 2000 total points
ID: 33446640
Your parameters are in the wrong order.
Unless you've explicitly gone with named parameters in your code - then you need to be consistent with the order.
With qQuery
    .ActiveConnection = dbMAIN
    .CommandType = adCmdStoredProc
    .CommandText = "UpdateClientDetails"
    .Parameters.Append .CreateParameter("NewNumber", adVarWChar, adParamInput, Len("MyNewClientNumber"), "MyNewClientNumber")
    .Parameters.Append .CreateParameter("RenumberDate", adDate, adParamInput,  8, "MyNewDate")
    .Parameters.Append .CreateParameter("OldNumber", adVarWChar, adParamInput, Len("BWC00002"), "BWC00002")
    .Execute
End With

If you wanted to be more predictable then define your parameters in the query:

PARAMETERS  [NewNumber] Text(50), [NewDate] DateTime, [OldNumber] Text;
UPDATE ClientNumbers SET ClientNumbers.ArtClientNumber = [NewNumber], ClientNumbers.RenumberDate = [NewDate]
WHERE (((ClientNumbers.ArtClientNumber)=[OldNumber]));
That way you are being very clear about the order of parameters.
The alternative is to specify named parameters in your code.

With qQuery
    .ActiveConnection = dbMAIN
    .CommandType = adCmdStoredProc
    .CommandText = "UpdateClientDetails"
    .NamedParameters = True
    .Parameters.Append .CreateParameter("NewNumber", adVarWChar, adParamInput, 50, "MyNewClientNumber")
    .Parameters.Append .CreateParameter("NewDate", adDate, adParamInput,  8, "MyNewDate")
    .Parameters.Append .CreateParameter("OldNumber", adVarWChar, adParamInput, Len("BWC00002"), "BWC00002")
    .Execute
End With


When you can create them in any order you like.
Note that you need ADO 2.6 or newer referenced to support that.
Note that you parameter named now have to match exactly. (They didn't all do so before, but that didn't matter).
Cheers.
0
 

Author Closing Comment

by:MonteDelMar
ID: 33448270
Brilliant.  Thank you so much.
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 33448339
No problemo
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question