Solved

Excel VBA MySql database update not working

Posted on 2009-04-09
3
470 Views
Last Modified: 2012-06-21
The code below returns -3 for the absoluteposition, 'one' just before the update of the first field and fails with runtime error 3021

If the recordset is not eof, I cannot understand the error
Dim oConn As ADODB.Connection

   Dim rs As ADODB.Recordset

   Set oConn = New ADODB.Connection

   Set rs = New ADODB.Recordset

    oConn.Open "driver={MySQL ODBC 5.1 Driver}; server=localhost; uid=xxxxx; pwd=xxxx; database=intranet; option=3"
 

tName = "products"
 

'Set rs = oConn.OpenRecordset(tName, dbOpenTable)

rs.CursorType = adOpenKeyset

rs.CursorLocation = adUseClient

rs.LockType = adLockOptimistic
 

rs.Open tName, oConn, adOpenKeyset, adLockOptimistic
 

'rs.Execute ("Delete Products.* from Products")

' get all records in a table

r2 = 2 ' the start row in the worksheet

Do While Cells(r2, 1) <> ""

   rs.MoveFirst
 

   rs.Find "PItemRef=" & Chr(34) & Cells(r2, 4).Value & Chr(34)

   MsgBox (rs.AbsolutePosition)
 

   If (rs.EOF) And (rs.BOF) Then
 

      rs.AddNew

      rs.Fields("PItemID") = Cells(r2, 1).Value

      rs.Fields("PProductID") = Cells(r2, 2).Value

      rs.Fields("PProductName") = Cells(r2, 3).Value

      rs.Fields("PItemRef") = Cells(r2, 4).Value

      rs.Fields("PItemName") = Cells(r2, 5).Value

      rs.Fields("PCategoryName") = Cells(r2, 6).Value

      cats = Split(Cells(r2, 6).Value, "/")

      ID = DLookup("CatID", "Categories", "Category = " & Chr(34) & cats(0) & Chr(34) & " and ParentID=0")

      If UBound(cats) > 0 Then

          For x = 1 To UBound(cats)

              ID = DLookup("CatID", "Categories", "Category = " & Chr(34) & cats(x) & Chr(34) & " and ParentID=" & ID)

          Next

            

      End If

      rs.Fields("PSupplierID") = Cells(r2, 7).Value

      rs.Fields("PBarcode") = Cells(r2, 8).Value

      rs.Fields("PManfReference") = Cells(r2, 10).Value

      rs.Fields("PBaseCost") = Cells(r2, 11).Value

      rs.Fields("PCategoryID") = ID

   Else

      MsgBox ("one")

      rs.Fields("PItemID") = Cells(r2, 1).Value     <<<<fails here

      MsgBox ("two")

      rs.Fields("PProductID") = Cells(r2, 2).Value
 

      rs.Fields("PProductName") = Cells(r2, 3).Value

      rs.Fields("PItemRef") = Cells(r2, 4).Value

      rs.Fields("PItemName") = Cells(r2, 5).Value

      rs.Fields("PCategoryName") = Cells(r2, 6).Value
 

      cats = Split(Cells(r2, 6).Value, "/")

      ID = DLookup("CatID", "Categories", "Category = " & Chr(34) & cats(0) & Chr(34) & " and ParentID=0")

      If UBound(cats) > 0 Then

          For x = 1 To UBound(cats)

              ID = DLookup("CatID", "Categories", "Category = " & Chr(34) & cats(x) & Chr(34) & " and ParentID=" & ID)

          Next

            

      End If

      rs.Fields("PSupplierID") = Cells(r2, 7).Value

      rs.Fields("PBarcode") = Cells(r2, 8).Value

      rs.Fields("PManfReference") = Cells(r2, 10).Value

      rs.Fields("PBaseCost") = Cells(r2, 11).Value

      rs.Fields("PCategoryID") = ID

   End If
 

   rs.Update
 

    r2 = r2 + 1

Loop

rs.Close

Set rs = Nothing

Open in new window

0
Comment
Question by:pzh20
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
StellanRosengren earned 126 total points
ID: 24116575
Hi pzh20
The line
If (rs.EOF) And (rs.BOF) Then
tests if the recordset is empty (otherwise both EOF and BOF cannot be true)
maybe you meant to use
If (rs.EOF) Or (rs.BOF) Then
since you get rs.EOF when the item was not found with rs.Find
absoluteposition for EOF is -3

Regards,
Stellan
0
 
LVL 13

Assisted Solution

by:StellanRosengren
StellanRosengren earned 126 total points
ID: 24158519
Hi again,
Maybe I was not clear enough.
The expression
rs.EOF And rs.BOF
is True only if rs is empty (no records)
and what you want to test is if no record was found
using rs.Find
If no record was found, only rs.EOF is True

So, please try to change to

If rs.EOF Then
 rs.AddNew
 ...


Kind regards,
Stellan

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 7 Share with concurrent edits(Excel) 3 32
xampp tool 12 24
Automating Excel Weekly Report 13 54
Access Excel export not behaving 2 25
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now