Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2006, 09:54 AM
Authorized User
 
Join Date: Dec 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Spliting a record

Hello,
I have a record with 9 fields and one of these is an amount.
I want to split this record in two so that everything remains the same but the amount on both records is divided by 2.
Ex.
ID | DATE | AMOUNT
------------------------
1 |10/10/2006 | 100 €

>> Click on SPLIT BUTTON <<<

1 |10/10/2006 | 50 €
2 |10/10/2006 | 50 €

I know it is tough but it would really be useful to me.
Sven

 
Old October 24th, 2006, 10:22 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Hi,

There are many ways to do this. You can use an update query and an append query and run them in sequence, or you can use VBA. Since this is in the VBA forum, how about:

Dim iID As Integer
Dim dtDate As Date
Dim cAmount As Currency
'Since I do mostly ADO, here is some code, but others can show you DAO
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sSQL As String

iID = Me.ID
dtDate = Me.Date 'bad field name
cAmount = Me.Amount/2

sSQL = "SELECT * FROM MyTable WHERE [ID] = " & iID

'Open Connection to database
Set cn = New ADODB.Connection
With cn
    .ConnectionString = CurrentProject.Connection
    .CursorLocation = adUseClient
End With
cn.Open "Provider=..." 'or Access provider statement - use DAO

sSQL = "SELECT * FROM Employee WHERE [EmployeeID] = " & pEmpID
sSQLU = "SELECT * FROM Username WHERE [Username] = '" & pUserName & "'"

'Open Recordset on MtTable
Set rs = New ADODB.Recordset
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
rs.Open sSQL, cn

rs("Amount") = cAmount
rs.Update

rs.AddNew
rs("Date") = dtDate
rs("Amount") = cAmount
rs.Update

rs.Close
cn.Close

Okay, that is how you would do it on a SQL Server, just use DAO for Access. And note, that in MyTable, I am sure there are other fields that relate this record to some other master record, so add that data in the rs.AddNew using the proper variables.

I hope that helps.

mmcdonal
 
Old October 24th, 2006, 10:24 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Sorry, you can tell I copied and modified the code. Remove these two lines:

sSQL = "SELECT * FROM Employee WHERE [EmployeeID] = " & pEmpID
sSQLU = "SELECT * FROM Username WHERE [Username] = '" & pUserName & "'"



mmcdonal
 
Old October 25th, 2006, 03:10 AM
Authorized User
 
Join Date: Dec 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks alot!
I adapted the code slightly and it now works perfectly!
Sven






Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete a record row, not just the record. Coby Access VBA 1 April 30th, 2007 06:29 AM
spliting database in record level dhua SQL Server 2000 5 January 30th, 2007 07:32 AM
Spliting a data table Hennie Access 7 July 21st, 2006 06:45 AM
Record locking - user needs the next queued record cbtoolkit SQL Server 2000 0 December 6th, 2004 08:29 AM
Sub record not associating with Main record Ron V Access 1 August 31st, 2004 09:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.