Syntax Error with DSum Statement (1 Viewer)

RodShinall

Registered User.
Local time
Today, 11:09
Joined
Dec 21, 2006
Messages
32
I am trying to build a personal check register application and I need to write a DSum statement with two criteria as the control source of an unbound text box control which give me a running balance and I can't get the syntax correct. TransactionAmount and TransactionNumber are both number fields and Account is short text. I've tried replacing the double quotes with single quotes but still get an invalid syntax error. Any help would be greatly appreciated.

txtBalance.ControlSource = "=DSum("[TransactionAmount]","tblAccountRegister","[TransactionNumber] <= " & txtTransactionNumber.Value & " AND [Account] = '" & mstrAccountNumber & "')"
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:09
Joined
May 21, 2018
Messages
8,525
are the Transaction Number and Account Numbers really numbers or are they numbers stored in a text field. If text you need single quotes delimiter around the value.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:09
Joined
May 7, 2009
Messages
19,230
try:

txtBalance.ControlSource = "=DSum(""[TransactionAmount]"",""tblAccountRegister"",""[TransactionNumber] <= " & txtTransactionNumber.Value & " AND [Account] = '" & mstrAccountNumber & "'"")"
 

RodShinall

Registered User.
Local time
Today, 11:09
Joined
Dec 21, 2006
Messages
32
are the Transaction Number and Account Numbers really numbers or are they numbers stored in a text field. If text you need single quotes delimiter around the value
 

RodShinall

Registered User.
Local time
Today, 11:09
Joined
Dec 21, 2006
Messages
32
are the Transaction Number and Account Numbers really numbers or are they numbers stored in a text field. If text you need single quotes delimiter around the value.
TransactionNumber is a number stored in a Number field. Account is a text string stored in a text field.
are the Transaction Number and Account Numbers really numbers or are they numbers stored in a text field. If text you need single quotes delimiter around the value.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:09
Joined
May 21, 2018
Messages
8,525
I had to play with it to get it rigth
Code:
Public Sub test()
  Dim str As String
  Dim txttransactionnumber As Long
  txttransactionnumber = 1234
  Dim mstrAccountNumber As String
  mstrAccountNumber = "abcd"
  str = "=DSum(""[TransactionAmount]"",""tblAccountRegister"",""[TransactionNumber] <= " & txttransactionnumber & " And [Account] = '" & mstrAccountNumber & "')"
  Debug.Print str
End Sub
=DSum("[TransactionAmount]","tblAccountRegister","[TransactionNumber] <= 1234 And [Account] = 'abcd')
 

CJ_London

Super Moderator
Staff member
Local time
Today, 17:09
Joined
Feb 19, 2013
Messages
16,610
why are you assigning the controlsource in code? why not just include it in design view

=DSum("[TransactionAmount]","tblAccountRegister","[TransactionNumber] <= " & txtTransactionNumber & " AND [Account] = '" & mstrAccountNumber & "'")
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:09
Joined
May 21, 2018
Messages
8,525
That was my next question because doing that in code is kind of a PITA. Also you could simply set the value
 

RodShinall

Registered User.
Local time
Today, 11:09
Joined
Dec 21, 2006
Messages
32
try:

txtBalance.ControlSource = "=DSum(""[TransactionAmount]"",""tblAccountRegister"",""[TransactionNumber] <= " & txtTransactionNumber.Value & " AND [Account] = '" & mstrAccountNumber & "'"")"
Thanks for your reply. Your code does not produce an error but does not give me the running total.
 

RodShinall

Registered User.
Local time
Today, 11:09
Joined
Dec 21, 2006
Messages
32
I had to play with it to get it rigth
Code:
Public Sub test()
  Dim str As String
  Dim txttransactionnumber As Long
  txttransactionnumber = 1234
  Dim mstrAccountNumber As String
  mstrAccountNumber = "abcd"
  str = "=DSum(""[TransactionAmount]"",""tblAccountRegister"",""[TransactionNumber] <= " & txttransactionnumber & " And [Account] = '" & mstrAccountNumber & "')"
  Debug.Print str
End Sub
=DSum("[TransactionAmount]","tblAccountRegister","[TransactionNumber] <= 1234 And [Account] = 'abcd')
Thanks for your reply. I'll try this.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 12:09
Joined
Feb 19, 2002
Messages
43,233
Running sum is a property of the control on a report and is a significantly more efficient way to produce a running sum than using a query.

PS - Using Me.ControlName gives you intellisense immediately and the Me. tells Access and people that you are referring to a property of the active form or report.
PPS - the .Value property is the default and so can be omitted and most people do for brevity so Me.ControName and Me.ControlName.Value are equivalent.
 

Users who are viewing this thread

Top Bottom