By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

Value determined by VBA coding does not show in table

P: 4
So I have a worksheet on access with my VBA code along with a form and a table on access. All three of these are connected and work off of each other. My problem is that I have a value (T-Score) that is calculated by VBA with an If Then statement but it does not show the value on the table with the rest of the data. Anyone have any idea how to connect the two?
Jan 9 '14 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 5K+
P: 5,397
You need to show your code.[*]> Before Posting (VBA or SQL) Code
Jan 9 '14 #2

P: 4
Expand|Select|Wrap|Line Numbers
  1. Sub Boys7to12()
  2.    If [Total Score] >= 39 Then
  3.       [T-Score] = 90
  4.       ElseIf [Total Score] = 38 Then
  5.          [T-Score] = 89
  6.          ElseIf [Total Score] = 37 Then
  7.             [T-Score] = 88
  8.    End If
  9. End Sub
(in the actual coding, it continues the same until 0.
Jan 9 '14 #3

Expert Mod 5K+
P: 5,397
1) This is a sub not a function; therefor, it's not supposed to return a value.

2) There are neither recordsets opened nor SQL executed to update a field

3) Where are you attempting to use this code from: a form, a calculated control, a table field?

I take it that you are fairly new to database design, Access, and VBA?
Jan 9 '14 #4

P: 4
1) so if I instead use a code like...
Expand|Select|Wrap|Line Numbers
  1. Public Function getAgePhrase(ByVal age As Integer) As String 
  2.     If age > 60 Then Return "Senior" 
  3.     If age > 40 Then Return "Middle-aged" 
  4.     If age > 20 Then Return "Adult" 
  5.     If age > 12 Then Return "Teen-aged" 
  6.     If age > 4 Then Return "School-aged" 
  7.     If age > 1 Then Return "Toddler" 
  8.     Return "Infant" 
  9. End Function
would that allow it to return the corresponding value?

2) I don't quite understand when to properly use SQL. Is it necessary here?

3) all of the above. the form is interactive for others to input their necessary data and then it connects to a table field where the data is simplified and displayed in an easy to read format.

I am pretty new to all of it and only know basics but I am trying to learn. Thank you so much for your help and patience.
Jan 9 '14 #5

Expert Mod 5K+
P: 5,397
1) So in a query or the control source for a form control:
Expand|Select|Wrap|Line Numbers
  1. Query:
  2. FieldName: getAgePhrase([AgeField])
In a control's property sheet, data tab, property [Control Source], provided the [AgeField] was in the form's record set or available in an unbound text box then:
Expand|Select|Wrap|Line Numbers
  1. =getAgePhrase([AgeField])
You would not normally use this in a table.

>>> I would also not hard code the example you've given for the age range - it's hard to maintain. Instead I would have a table with these text results and a query that returns the value based on the criteria see (2). You might have to redesign the table structure as well as often when I see things like this hardcoded there is an underlying weakness in the database design.

2) The answer is both yes and no, see (1). If you use the function in a query, via the query editor (QE), you are using SQL, just that Access has hidden it behind a pretty dialog box. You can see the SQL script by right clicking in the table area of the QE and selecting SQL-View. (Select design view to go back to the happy place (^_^) )

3)That was the impression I had of what was happening. Even though everything is related, it's not always so direct which can be confusing and frustrating (even to us old-hands)! So in your case using the vba in order for the VBA (or Macro which is a 2nd language avaiable in Access) to have an effect on a record set there must be an explict value change. This can happen in a form if the control is bound to table/query and the VBA alters the value of bound control, the VBA opens a recordset and updates the values (can even be the form's recordset), the VBA uses the execute method on an action SQL (such as INSERT or UPDATE).

4) I'm going to PM you my standard list of what I hope are helpfull links. There are some tutorials in this that I highly recommend you take a careful look at (and hopefully work thru the hands-on one). There is a sublink in the hands-on one that works thru database design (in ACC2003; however, the concepts still hold true) that you might find of great help.
Jan 9 '14 #6

P: 4
Thank you so much!! You have been such a great help! I will definitely look through the links you send.
I will play around with all the information you have given me and hopefully work it out. I really appreciate all your time. :)
Jan 9 '14 #7

Post your reply

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