473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Use a recordset to add 2 rows (transactions) in same table

547 512MB
I have a form where i issue or receive stock to/from medical clinics.
Currently you select the transaction type ie transfer_out or transfer_in and add the record.
I would like to have the following happening simultaneously
If clinic A supply stock to clinic B then Clinic A = Transfer _out and Clinic B = Transfer_In for the same item, transaction date, etc etc and not 2 separate records to be added by the datacapturer.
This is the VBA that i currently use for 1 record added at a time. In the background i will then add or subtract stock accordingly for stock on hand calculations.
The "ID" of transfer_out is 3 and "transfer_in" = 4 and the other "id" transact should be "else"
I imagine the following has to be worked into the code:
Expand|Select|Wrap|Line Numbers
  1. if me.transact = "3" then run the code below and repeat the code using me.transact = 4 else "run only the code below once for me.transact that are not equal to 3 or 4 and also add the edit funtion". 
  2.  
My current code
Expand|Select|Wrap|Line Numbers
  1.   Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim strSql As String
  4.  
  5. Set MyDB = CurrentDb
  6. strSql = "SELECT * FROM Fis_CaptDataT WHERE [DataId] = " & Me![DataID]
  7.  
  8. 'See if the Data has already been Captured, if not, Add, not Edit the Record
  9. If DCount("*", "Fis_CaptDataT", "[DataId] = " & Me![DataID]) = 0 Then     'NOT Captured/ADD
  10.   Set rst = MyDB.OpenRecordset("Fis_CaptDataT", dbOpenDynaset, dbAppendOnly)
  11.     With rst
  12.       .AddNew
  13. ![Client_lookup] = Me![Client_lookup]
  14. ![InvoiceDate] = Me![InvoiceDate]
  15. ![ExpiryDate] = Me![ExpiryDate]
  16. ![Item_Lookup] = Me![Item_Lookup]
  17. '![CPrice] = Me![Price]
  18. ![CPrice] = Me![CPrice]
  19. ![Providers] = Me![Providers]
  20. ![Supplier] = Me![Supplier]
  21. ![Order_No] = Me![Order_No]
  22. ![Transact] = Me![Transact]
  23. ![BatchNo] = Me![BatchNo]
  24.  
  25. .Fields("Transaction" & CStr(Me![Transaction])) = Me![InvoiceQty]
  26.  .update
  27.   End With
  28.   Else        'Data Captured, so Edit the Recordset
  29.   Set rst = MyDB.OpenRecordset(strSql, dbOpenDynaset)
  30.     With rst
  31.     .Edit
  32. ![Client_lookup] = Me![Client_lookup]
  33. ![InvoiceDate] = Me![InvoiceDate]
  34. ![ExpiryDate] = Me![ExpiryDate]
  35. ![Item_Lookup] = Me![Item_Lookup]
  36. '![CPrice] = Me![Price]
  37. ![CPrice] = Me![CPrice]
  38. ![Providers] = Me![Providers]
  39. ![Supplier] = Me![Supplier]
  40. ![Order_No] = Me![Order_No]
  41. ![Transact] = Me![Transact]
  42. ![BatchNo] = Me![BatchNo]
  43. .Fields("Transaction" & CStr(Me![Transaction])) = Me![InvoiceQty]
  44. .update
  45.     End With
  46.    End If
  47.    rst.close
  48.    Set rst = Nothing
  49. Set MyDB = Nothing
  50. If [Forms]![CaptureF]![finditem2].Value = "-1" Then
  51. DoCmd.GoToControl ("finditem")
  52. End If
Any ideas please?
Jun 25 '13 #1

✓ answered by ADezii

Here is an oversimplification of how I see your process (Transferring a Quantity of Stock for a given Client/Stock):
  1. Data before Transfer (25 units of ABC Stock from Client A to Client B):
    Expand|Select|Wrap|Line Numbers
    1. Client     Stock    Quantity
    2. Client A    ABC       100
    3. Client B    ABC        25
    4.  
  2. Code to effect Transfer using Recordsets:
    Expand|Select|Wrap|Line Numbers
    1. Dim strClientFROM As String
    2. Dim strClientTO As String
    3. Dim lngQuantityToTransfer As Long
    4. Dim MyDB As DAO.Database
    5. Dim rstFROM As DAO.Recordset
    6. Dim rstTO As DAO.Recordset
    7. Dim strStock As String
    8. Dim strSQL1 As String
    9. Dim strSQL2 As String
    10.  
    11. strClientFROM = "Client A"
    12. strClientTO = "Client B"
    13. strStock = "ABC"
    14. lngQuantityToTransfer = 25
    15.  
    16. strSQL1 = "SELECT * FROM tblClients WHERE [Client] = '" & strClientFROM & "' AND [Stock] = '" & _
    17.            strStock & "'"
    18. strSQL2 = "SELECT * FROM tblClients WHERE [Client] = '" & strClientTO & "' AND [Stock] = '" & _
    19.            strStock & "'"
    20.  
    21. Set MyDB = CurrentDb
    22. Set rstFROM = MyDB.OpenRecordset(strSQL1, dbOpenDynaset)
    23. Set rstTO = MyDB.OpenRecordset(strSQL2, dbOpenDynaset)
    24.  
    25. '******************** Enclose within a Transaction ********************
    26. With rstFROM
    27.   .Edit
    28.     ![Quantity] = ![Quantity] - lngQuantityToTransfer
    29.   .Update
    30. End With
    31.  
    32. With rstTO
    33.   .Edit
    34.       rstTO![Quantity] = rstTO![Quantity] + lngQuantityToTransfer
    35.   .Update
    36. End With
    37. '**********************************************************************
    38.  
    39. rstTO.Close
    40. rstFROM.Close
    41. Set rstTO = Nothing
    42. Set rstFROM = Nothing
    43.  
  3. Post Transfer Data:
    Expand|Select|Wrap|Line Numbers
    1. Client    Stock    Quantity
    2. Client A    ABC       75
    3. Client B    ABC       50
    4.  

4 1435
ADezii
8,834 Expert 8TB
I think that the extremely important point you are missing is that if your want both Transfer IN and OUT Operations to happen concurrently, they must be treated as a single Transaction, and programmed as such. Imagine a situation where Stock from Client A is supposed to be transferred to Client B. Client A successfully transfers its Stock, an error occurs, resulting in Client B not receiving the same Stock. Either the entire Transaction must either succeed or fail as a single Atomic Unit.
Jun 25 '13 #2
neelsfer
547 512MB
Thx Adezi i have had that plan in mind as a single transaction.

When A issues stock, both transactions must happen concurrently and should the quantity issued changes, then it must also update both rows/records.
Client A issueing = me.Client_lookup
and client B receiving = me.Providers.
mainform = CaptureF
On the main form i will have 4x comboboxes - Client issueing + Transact and Client Receive + transact for the datacapturer to select, before capturing.

I have been thinking that perhaps i must rather use an append query on completion of the transaction, if i cannot use the recordset method? What do you suggests?
Jun 25 '13 #3
ADezii
8,834 Expert 8TB
Here is an oversimplification of how I see your process (Transferring a Quantity of Stock for a given Client/Stock):
  1. Data before Transfer (25 units of ABC Stock from Client A to Client B):
    Expand|Select|Wrap|Line Numbers
    1. Client     Stock    Quantity
    2. Client A    ABC       100
    3. Client B    ABC        25
    4.  
  2. Code to effect Transfer using Recordsets:
    Expand|Select|Wrap|Line Numbers
    1. Dim strClientFROM As String
    2. Dim strClientTO As String
    3. Dim lngQuantityToTransfer As Long
    4. Dim MyDB As DAO.Database
    5. Dim rstFROM As DAO.Recordset
    6. Dim rstTO As DAO.Recordset
    7. Dim strStock As String
    8. Dim strSQL1 As String
    9. Dim strSQL2 As String
    10.  
    11. strClientFROM = "Client A"
    12. strClientTO = "Client B"
    13. strStock = "ABC"
    14. lngQuantityToTransfer = 25
    15.  
    16. strSQL1 = "SELECT * FROM tblClients WHERE [Client] = '" & strClientFROM & "' AND [Stock] = '" & _
    17.            strStock & "'"
    18. strSQL2 = "SELECT * FROM tblClients WHERE [Client] = '" & strClientTO & "' AND [Stock] = '" & _
    19.            strStock & "'"
    20.  
    21. Set MyDB = CurrentDb
    22. Set rstFROM = MyDB.OpenRecordset(strSQL1, dbOpenDynaset)
    23. Set rstTO = MyDB.OpenRecordset(strSQL2, dbOpenDynaset)
    24.  
    25. '******************** Enclose within a Transaction ********************
    26. With rstFROM
    27.   .Edit
    28.     ![Quantity] = ![Quantity] - lngQuantityToTransfer
    29.   .Update
    30. End With
    31.  
    32. With rstTO
    33.   .Edit
    34.       rstTO![Quantity] = rstTO![Quantity] + lngQuantityToTransfer
    35.   .Update
    36. End With
    37. '**********************************************************************
    38.  
    39. rstTO.Close
    40. rstFROM.Close
    41. Set rstTO = Nothing
    42. Set rstFROM = Nothing
    43.  
  3. Post Transfer Data:
    Expand|Select|Wrap|Line Numbers
    1. Client    Stock    Quantity
    2. Client A    ABC       75
    3. Client B    ABC       50
    4.  
Jun 25 '13 #4
neelsfer
547 512MB
thx Adezi i will try it out tomorrow
Jun 25 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jason | last post by:
I need to populate a table from several sources of raw data. For a given security (stock) it is possible to only receive PARTS of information from each of the different sources. It is also...
6
by: Roy Gourgi | last post by:
Hi, I am trying to add a row to my table but I get the error message "invalid column name SOBN and BN1" on this statement. Basically, I am trying to add the row into the same table that I am...
3
by: jenner4000 | last post by:
Hi, My first post in this group, so hope some of you can help me. My table: "answer" answerID answer(int) questionID(int) userID(int)
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
1
by: sudhendra | last post by:
I have a table supertask; which has 24 rows. The table as a primary_key called id. I would like just recursively copy rows into same table. supertask_id is auto generated. So All I need to do is...
2
by: sudhaMurugesan | last post by:
Hi, I have a table like this. IOMode Date EmployeeID EmpName O 2007-02-28 16:46:00.000 FI0001258 M.MANIGANDAN I 2007-02-28...
2
by: twink | last post by:
I am trying to access certain rows in a table in a combo box. I get this error (Run-time error '3265' Item not found in this collection) Here is the code. I am new to VBA so please excuse me. Here is...
4
bugboy
by: bugboy | last post by:
I have a foreign key table for defining many to many relationships in two other tables. This table (MapTable) has two foreign keys, aID and bID. MapTable aID | bID I start the query knowing 2...
22
by: DreamersDelight | last post by:
Hi, I'm stuck on this problem and I can't find a sollution. I'm going to try and explain this step by step. 1 After certain rows get updated with a certain value. I don't know wich rows in...
2
by: Gordon Padwick | last post by:
I am developing an Access application using Access 2010 running under Windows 7. The database has some large tables containing many thousands of rows. I want to display the number of rows in these...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.