Re: Update a record in an SQL table
- From: richardb <richardb@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 13 Dec 2006 08:50:01 -0800
Douglas:
Thank you, that worked. Now that I know the next batch number, I need to ADD
a batch row to the Batch table. For example, I got this SQL string by
creating an Access Append query (if otherwise OK, do I need the aliases?):
INSERT INTO Batch ( BatchNumber, status )
SELECT "29" AS BatchNum, "O" AS Status;
I will assemble this string each time and run it the same as your example
for the UPDATE, but use this INSERT SQL string. Will that be OK?
"Douglas J. Steele" wrote:
There's no need for the recordset at all..
Dim mydb As DAO.Database
Dim myq As DAO.QueryDef
Dim sqltext As String, strConnect As String
Set mydb = CurrentDb
Set myq = mydb.CreateQueryDef("")
sqltext = "UPDATE batchbmp SET szValue_AN = " & BatchNumber & ";"
strConnect = "ODBC;DSN= PPM_700;;" & _
"Network=DBMSSOCN;Trusted_Connection=Yes"
With myq
.Connect = strConnect
.ReturnsRecords = False
.SQL = sqltext
.Execute dbFailOnError
End With
UpdateBatchNumber_Exit:
myq.Close
Set myrs = Nothing: Set myq = Nothing: Set mydb = Nothing
sqltext = vbNullString: strConnect = vbNullString
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3151
MsgBox "Information not found due to ODBC connection failure." &
vbCrLf & _
"Open Maintenance, Lookup and Correct DSN",
_
vbInformation, Err.Number & " - " & Error$
Case Else
MsgBox Err.Number & " - " & Err.Description, vbInformation,
"Error in GetBatchNumber"
End Select
Resume UpdateBatchNumber_Exit
End Sub
I'm not sure about your Connect string, though: the two consecutive
semi-colons doesn't seem right. I would also have expected that your DSN
would already contain the information about Network and Trusted Connection,
so that all you'd need would be:
strConnect = "ODBC;DSN= PPM_700"
You might also consider going DSN-less, to simplify distributing the
application to multiple workstations. In that case, your connect string
would be something like:
strConnect = "ODBC;Driver={SQL Server};Server=MyServerName;" & _
"Database=MyDatabaseName;Trusted_Connection=yes"
or
strConnect = "ODBC;Driver={SQL Server};Network=DBMSSOCN;" & _
"Server=MyServerName;Database=MyDatabaseName;" & _
"Trusted_Connection=yes"
(replace MyServerName and MyDatabaseName with the correct names)
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)
"richardb" <richardb@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A773A884-AAA0-4083-B4FE-B5EAC1E924E2@xxxxxxxxxxxxxxxx
I am trying to update a value in an SQL table, but can't seem to get all
the
pieces correct. Would appreciate advice. Here is the listing:
Public Sub UpdateBatchBMP(BatchNumber As Long)
On Error GoTo ErrorHandler
Dim mydb As DAO.Database
Dim myq As DAO.QueryDef
Dim myrs As DAO.Recordset
Dim sqltext As String, strConnect As String
Set mydb = CurrentDb
Set myq = mydb.CreateQueryDef("")
sqltext = "UPDATE batchbmp SET szValue_AN = " & BatchNumber & ";"
strConnect = "ODBC;DSN= PPM_700;;" & _
"Network=DBMSSOCN;Trusted_Connection=Yes"
With myq
.Connect = strConnect
.ReturnsRecords = False
.SQL = sqltext
Set myrs = .OpenRecordset (??? here I'm not sure)
Set myrs = .Updatable (??? or here)
End With
With myrs
' ... code to work with recordset ...
'.MoveFirst
!szValue_AN = Str$(BatchNumber)
.Update
.Close
End With
UpdateBatchNumber_Exit:
myq.Close
Set myrs = Nothing: Set myq = Nothing: Set mydb = Nothing
sqltext = vbNullString: strConnect = vbNullString
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3151
MsgBox "Information not found due to ODBC connection failure."
&
vbCrLf & _
"Open Maintenance, Lookup and Correct DSN",
_
vbInformation, Err.Number & " - " & Error$
Case Else
MsgBox Err.Number & " - " & Err.Description, vbInformation,
"Error in GetBatchNumber"
End Select
Resume UpdateBatchNumber_Exit
End Sub
Thank you...
- Follow-Ups:
- Re: Update a record in an SQL table
- From: Douglas J. Steele
- Re: Update a record in an SQL table
- References:
- Re: Update a record in an SQL table
- From: Douglas J. Steele
- Re: Update a record in an SQL table
- Prev by Date: Re: Different version Access
- Next by Date: Re: Update a record in an SQL table
- Previous by thread: Re: Update a record in an SQL table
- Next by thread: Re: Update a record in an SQL table
- Index(es):
Relevant Pages
|