Solved

Update order records with item information

Posted on 2011-09-10
13
316 Views
Last Modified: 2012-05-12
I have two tables one containing order information and another containing item information. one order to many items. I need to update the order table with the item information. Please post the VBA on how to loop through the item table and update the order table.

Order table fields look like this:
Name,qty1,qty2,qty3,qty4,qty5,qty6,sku1,sku2,sku3,sku4,sku5,sku6,uniqueid

item table fields look like this:
uniqueid,sku,qty

within the item table there could be up to 6 item records per uniqueid.
0
Comment
Question by:Dalexan
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 16

Expert Comment

by:Sheils
Comment Utility
Usually a simply form/subform set should do the job. No VBA required. Noting the field names qty1,qty2,qty3.... I suspect that the problem is with your table design. Can you explain what are these fields used for. Even better if you could post a sample of you DB

0
 

Author Comment

by:Dalexan
Comment Utility
db1.mdb

I have uploaded a sample database to be used to show an example of what the tables look like. They are actually much bigger with many more fields. I need to know how to update the order records with the item information using VBA. The actual tables are linked tables to a larger mysql remote database.
0
 
LVL 16

Expert Comment

by:Sheils
Comment Utility
As I mentioned in the previous post, I think that there is a problem with your table design.  Instead of developing complex code to cater for poor design it's better to fix the design, which if not fix will give you a lot of problem as you progress with the development.

So Please answer the following question

why do you have qty1,qty2,qty3.... and sku1,sku2,sku3.... in your order table.

I am stressing on this because I would normally expect something like

tblOrder
--------
OrderID, Name,Date

tblItem
ItemID,OrderID,sku,qty
0
 

Author Comment

by:Dalexan
Comment Utility
These are linked tables in a database that I cannot modify. I have to loop through the order table get the uniqueid, match it to the corresponding uniqueid in the items table and update the order records qty and sku values with the values from the item table.

I want to stress again that I cannot modify the table structure, these tables are part of a much larger complex system and are used for other process's. My task is to update the quantity's 1 through 6 and sku's 1 through 6 in the order table.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
Dalexan,

I agree with sb9.

While it may be true that you cannot modify the tables, it is also true that they (or at least this one table) are not "Normalized".

So the need here is for the original designer of these tables to "modify" them.

Left in this "un-normalized" state this table will forever require workarounds to get it to function like a true "Normalized" table.
(Like your situation here)

Other disadvantages of this design will be that it may have forced other tables to follow a similar design, and that getting summaries, or "Range Data" out of a table like this will be difficult.

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
I am not saying that this is impossible, or that we won't help you...

It's just unfortunate that you have been given an inefficient design and been asked to "make it work".
:-(

So sit tight, perhaps a workaround will be forthcoming...
;-)

Jeff
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Dalexan
Comment Utility
Thank you, I agree that is is very unfortunate that I have to work with this table structure. The short and simple explanation is that the order table is a temporary table that is used to export a FIXED FIELD text file that is imported into a mainframe system.....rex....or something like that. This may explain why this table is structured the way it is.

I'm just looking for some VBA code that will loop through the order record, get the unique id, then loop through the items matching that unique id and fill in qty1-6 and sku1-6 in the order record.

Thanks for your quick attention and comments.
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 260 total points
Comment Utility
It is unfortunate for a developer to have to work with a poorly designed, non-normalized table structure, but sometimes it does happen, usually in the circumstances you describe (exporting to a flat-file database).  Supposing that there are no more than 6 SKUs per ID in the Details table, this code should do the job:
Option Compare Database
Option Explicit

Public Sub UpdateOrders()
'Created by Helen Feddema 10-Sep-2011
'Last modified by Helen Feddema 10-Sep-2011

On Error GoTo ErrorHandler

   Dim rstOrders As DAO.Recordset
   Dim rstDetails As DAO.Recordset
   Dim lngID As Long
   Dim strQuery As String
   Dim strSQL As String
   Dim intCounter As Integer
   Dim strSKUField As String
   Dim strQtyField As String
   Dim lngCount As Long
   
   Set rstOrders = CurrentDb.OpenRecordset("DataTableOrders", dbOpenDynaset)
   strQuery = "qryTemp"
   
   Do While Not rstOrders.EOF
      'Data type conversion needed because the matching fields are of
      'different data types
      lngID = CLng(rstOrders![uniqueid])
      
      If lngID <> 0 Then
         strSQL = "SELECT * FROM DataTableItems WHERE " _
            & "[ord_id] = " & lngID & ";"
      End If
   
      Debug.Print "SQL for " & strQuery & ": " & strSQL
      lngCount = CreateAndTestQuery(strQuery, strSQL)
      Debug.Print "No. of items found: " & lngCount
      If lngCount > 0 Then
         intCounter = 1
         
         Set rstDetails = CurrentDb.OpenRecordset(strQuery)
         
         Do While Not rstDetails.EOF
            rstOrders.Edit
            strSKUField = "SKU" & CStr(intCounter)
            strQtyField = "QTY" & CStr(intCounter)
            rstOrders(strSKUField) = rstDetails![SKU]
            rstOrders(strQtyField) = rstDetails![odi_qty]
            intCounter = intCounter + 1
            rstOrders.Update
            rstDetails.MoveNext
         Loop
      End If
      
      rstOrders.MoveNext
   Loop
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in UpdateOrders procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Public Function CreateAndTestQuery(strTestQuery As String, _
   strTestSQL As String) As Long
'Created by Helen Feddema 28-Jul-2002
'Last modified 6-Dec-2009

On Error Resume Next
   
   Dim qdf As DAO.QueryDef
   Dim rst As DAO.Recordset
   
   'Delete old query
   CurrentDb.QueryDefs.Delete strTestQuery

On Error GoTo ErrorHandler
   
   'Create new query
   Set qdf = CurrentDb.CreateQueryDef(strTestQuery, strTestSQL)
   
   'Test whether there are any records
   Set rst = CurrentDb.OpenRecordset(strTestQuery)
   With rst
      .MoveFirst
      .MoveLast
      CreateAndTestQuery = .RecordCount
   End With
   
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   If Err.Number = 3021 Then
      CreateAndTestQuery = 0
      Resume ErrorHandlerExit
   Else
   MsgBox "Error No: " & Err.Number _
      & " in CreateAndTestQuery procedure; " _
      & "Description: " & Err.Description
   End If
   
End Function

Open in new window

0
 
LVL 31

Expert Comment

by:Helen_Feddema
Comment Utility
I had to convert the linking field to a Long, because it is text in one table and Long Integer in the other -- I assume you can't change that either.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
Comment Utility
If there could be more than 6 SKUs, you could add a few lines of code to stop at the 6th.
0
 
LVL 16

Expert Comment

by:Sheils
Comment Utility
I have added a temporary table and a test form to the sample that you post. Clicking the button on the temp table will populate the temporary table. If the result in the temporary table is what you would like to insert in the item table we can proceed further with this approach.
db1.mdb
0
 

Author Comment

by:Dalexan
Comment Utility
I will review this and get back tomorrow, thanks for your quick responses.
0
 

Author Closing Comment

by:Dalexan
Comment Utility
Thank you Helen, This is exactly what I was looking for. Works excellent.

Also thanks for the debug window prints. Makes it much easier to see whats happening when debugging.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

762 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

6 Experts available now in Live!

Get 1:1 Help Now