Access VBA- refer to table field.

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
819
Office Version
  1. 365
Platform
  1. Windows
Hi Everybody,

I have the following code and it works.

VBA Code:
Private Sub Amount_AfterUpdate()
Me.Recalc

If Me.Controls![Income/Expense] = "Income" Then

Me.Controls![Credit] = Me.Controls![Amount]

Else

Me.Controls![Debit] = Me.Controls![Amount]

End If

End Sub

I would like to adjust it so that I don't have 2 invisible controls on my form.

Details-

1. I have a subform called "frmCashdissection" (many side) and a main form called "frmCashtransaction" (one side).
2. I have a table called "tblCashdissection" with two fields called "Credit" and "Debit"
3. When "income/expenses" is chosen from a combo box ("frmCashdissection") and an "Amount" is entered I would like the result to go direct to the table and not via the relevant control on the form.

So my question is- How to I refer to a table and field within VBA?


Further any general comments in taking this further, to mimic a bank statement with a running balance. I can get the debits and credits into their respective fields. But can I use an append query/DSum to get the running balance and put it into 'tblCashdissection".

thanks,

FarmerScott
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If "Amount" is indeed a field in your recordsource, all you need is:
Me.Controls![Credit] = Amount
Most Access devs rename their controls to provide a clear distinction between fields and the controls sourced to each field.
So if a textbox sources to a field name "Amount", it will typically be renamed to txtAmount.
In any case, Access will implicitly determine which you are referring to by syntax.
 
Upvote 0
If the transaction is a debit, I multiply the amount by -1. I only use one field for amount.
 
Upvote 0
If "Amount" is indeed a field in your recordsource, all you need is:
Me.Controls![Credit] = Amount
Most Access devs rename their controls to provide a clear distinction between fields and the controls sourced to each field.
So if a textbox sources to a field name "Amount", it will typically be renamed to txtAmount.
In any case, Access will implicitly determine which you are referring to by syntax.

Hi Dataluver,

Yes "Amount" is a field on my form and in the source table (where the data is being stored).

What i want to do is 'bypass' the need to have extra controls (thou invisible) on my form. I was hoping I could do something like-

VBA Code:
If Me.Controls![Income/Expense] = "Income" Then

Table("Cashdissection").Field name ("Credit") = Me.Controls![Amount]

Else

Table("Cashdissection").Field name ("Debit") = Me.Controls![Amount]

End If

So what i need is the VBA equivalent to Table("Cashdissection").Field name ("Debit")
thanks,

Farmerscott
 
Upvote 0
Does this work?

VBA Code:
If Me.Controls("Income/Expense").Value = "Income" Then

    Me.Credit = Me.Controls("Amount").Value

Else

    Me.Debit = Me.Controls("Amount").Value

End If
 
Upvote 0
Hi JonXL,

Your code looks to be working.

The only downside is that the values are not written back to the table until after the next record on the main form is active. This is not a big issue.

thanks for the help,

FarmerScott
 
Upvote 0
The only downside is that the values are not written back to the table until after the next record on the main form is active. This is not a big issue.

I think that is usual behavior - until you commit the record, it is in 'edit' mode and can be backed out of.

If you want to make sure the record is committed after updating this field, adding If Me.Dirty Then Me.Dirty = False will force the record to save after setting the control's value.

VBA Code:
If Me.Controls("Income/Expense").Value = "Income" Then

    Me.Credit = Me.Controls("Amount").Value

Else

    Me.Debit = Me.Controls("Amount").Value

End If

If Me.Dirty Then Me.Dirty = False
 
Upvote 0

Forum statistics

Threads
1,215,822
Messages
6,127,061
Members
449,357
Latest member
donna_koenig

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top