Text

Arwyn

Registered User.
Local time
Today, 12:42
Joined
Nov 1, 2006
Messages
20
I am trying to make a query to count text as a figure. I have entered on a form

H = Hoilday

A = Absent

Jan 1 Jan 2 Jan 3
H A H

If you make a query to add [jan1]+[jan2]+[jan3]

The result is HAH but I want it to be 2H 1A

Any ideas? It might not be a query I need!

Thanks
Arwyn
 
There will, no doubt, be other ways of doing this, but the following should work.

1) Construct the final string as you are now (e.g. HAHHHAAHA).
2) Make two copies of this final string
3) Use the Replace function to remove all letters H from one.
4) Use the Replace function to remove all latters A from the other.
5) Count the string lengths, using the Len() function.
6) Create your final answers in the form you want.

For example:

str_Overall = HAHHHAAHA
str_H = Replace(str_Overall,"A","") --- gives 'HHHHH'
str_A = Replace(str_Overall,"H","") --- gives 'AAAA'
str_Final_H = Len(str_H) & "H" --- gives '5H'
str_Final_A = Len(str_A) & "A" --- gives '4A'

Hope that helps.
 
Thanks Matt

Thanks for your reply to my problem.

But could you please tell me how I use the string?

Is it in the Criteria of a query, if so is it a select query.

By the way I am from Llanelli.

Thanks
Arwyn
 
Hwyl, Arwyn! Hope things are going well back home.

I would create some VBA code. You can either put this in the On Click event of a button (i.e. click the button and see the figures you want) or in something like the After Update event of the last field to be completed (so the user fills in the final value and is shown the total). You coulod also, if you wanted, create it as a function and call it after any field is updated, ensuring a running total is displayed.

Something like:

Dim str_Overall as String
Dim str_H as string
Dim str_A as String

str_Overall = [jan1] & [jan2] & .....etc.

str_H = Replace(str_Overall,"A","")
str_A = Replace(str_Overall,"H","")

str_Final_H = Len(str_H) & "H"
str_Final_A = Len(str_A) & "A"

FieldForHResult = str_Final_H
FieldForAResult = str_FinalA

Hope that helps.
 
Thanks Again

Thank you for trying to help me. What I am trying to do is to difficult for me to complete.

Thanks
Arwyn
 
In what form do you need the end result?
Are you trying to see the 5H 3A bits on a form, or on a report, or send the results to a table, or what?

And everything's hard until you know how to do it. ;) It took me a long time to get to the level I'm at - and that's not expert by any means.
 
My Problem

I have a form. It shows January to June. It is an attendance record for my employees.

For example

On my form employee Jane has decided to have this week as a hoilday.

Therefore on my form

Nov6 has h
Nov7 has h
Nov8 has h
Nov9 has h
Nov10 has h

But on November 1 she was absent from work, an a was entered in the box.

I do a Update query to add all the boxes Nov1 to Nov30 which will give a result of ahhhhh

What I want is 2 boxes giving me the result

in box 1 = 5h

in box 2 = 1a

Hope this helps

Thanks
Arwyn
 
Yep, I believe so (and forgive me if I was being twp :D ).

I think the way I described would do the trick. Apologies in advance if I explain something you already know.

1) Create two fields on the form - if you haven't already done so - one to display the H value, one to display the A value.
2) Create a button on the form (for now, I'll assume you want to see the totals when you click on a button. If this isn't the case, you can pretty much copy and paste the code to wherever you do want to run it).
3) Behind the OnClick event of the button, you'll be creating an Event Procedure (something to occur when the button is clicked). Right-click on the button and, under 'Properties', go to the Event tab. Click in the row labeled 'On Click' and you'll see three dots appear to the right.
4) Click on those and choose 'Code Builder'. The code window will show a blank sub procedure which will contain the code to run whenever the button is clicked.
5) Copy and paste the following, but remember to change the line shown in bold itallics to include all of the fields you want to count (presumably Jan1 to Jan31):
Code:
Dim str_Overall as String
Dim str_H as string
Dim str_A as String

[I][B]str_Overall = [jan1] & [jan2] & .....etc.[/B][/I]

str_H = Replace(str_Overall,"A","")
str_A = Replace(str_Overall,"H","")

str_Final_H = Len(str_H) & "H"
str_Final_A = Len(str_A) & "A"

FieldForHResult = str_Final_H
FieldForAResult = str_FinalA
6) Assuming the two fields you created to display the results are called FieldForHResult and FieldForAResult, you're fine. If not, alter the last two lines of code to whatever the fields are really called.
7) Save the changes made and go back to your form. Enter some H and A values into various fields and click on the button. Assuming the code works correctly, you should see the reults you want. If not, you may need to play with the code a little, but hopefully you can get the gist of it from the above.

Any more questions, let me know.

P.S. One of the good things you fast realise about this forum is that, if I've told you something incorrect, somebody will soon point it out.
 
Thank you

I have tried what you said but it did not work. So I am leaving the problem for today, try again tomorrow.

Thank you for all your help

Arwyn
 
Success

Matt

I have got your code to work perfectly.

But now I want to add another subject therefore

L = Late
A = Absent
H = Hoilday

Jan2 = L
Jan3 = A
Jan4 = H

I want 3 boxes on my form showing

Late = 1
Absent = 1
Holiday = 1

I am grateful for your help

Thanks
Arwyn
 
I think I see where I misinformed you. :o
Apologies for that, but glad to hear you were able to fix it.

Hopefully, this will be more successful.

1) Create a new field on the form (I've called it FieldForLResult)
2) Amend the existing code
Code:
Dim str_Overall as String
Dim str_H as string
Dim str_A as String
Dim str_L as String
Dim str_Final_H as Integer
Dim str_Final_A as Integer
Dim str_Final_L as Integer

str_Overall = [jan1] & [jan2] & .....etc.

str_H = Replace(str_Overall,"A","")
str_A = Replace(str_Overall,"H","")
str_L = Replace(str_Overall,"L","")

str_Final_H = Len(str_H) & "H"
str_Final_A = Len(str_A) & "A"
str_Final_L = Len(str_L) & "L"

FieldForHResult = str_Final_H
FieldForAResult = str_Final_A
FieldForLResult = str_Final_L

Fingers crossed.
 
Help yet again

Thank you for helping me again.

The new code that you have given me comes up with an error.

The error is in line

str_Final_H = Len(str_H) & "H"

Any ideas?

Thank you
Arwyn
 
Arwyn said:
Thank you for helping me again.

The new code that you have given me comes up with an error.

The error is in line

str_Final_H = Len(str_H) & "H"

Any ideas?

Thank you
Arwyn

Perhaps:

str_Final_H = Str(Len(str_H)) & "H"

If you still get no luck, try putting

Msgbox str_H

Just before the problematic line of code, so that you'll be able to see exactly what the variable str_H is set to.
What exactly is the error?
 
Error

Error is

Str_Final_H = Len(str_H) & "H"

I have tried what you suggested but the error comes up

Run Time Error 13

Type Mismatch

Thanks
Arwyn
 
I believe the problem is caused by trying to combine len(str_H), which is a number, with 'H', a letter.

Perhaps

1) Alter the declarations, so that
Code:
Dim str_Final_H as Integer
Dim str_Final_A as Integer
Dim str_Final_L as Integer
become
Code:
Dim ll_Final_H as Long
Dim ll_Final_A as Long
Dim ll_Final_L as Long

2) Alter the following from
Code:
str_Final_H = Len(str_H) & "H"
str_Final_A = Len(str_A) & "A"
str_Final_L = Len(str_L) & "L"

FieldForHResult = str_Final_H
FieldForAResult = str_Final_A
FieldForLResult = str_Final_L
to
Code:
ll_Final_H = Len(str_H)
ll_Final_A = Len(str_A)
ll_Final_L = Len(str_L)

FieldForHResult = str(ll_Final_H) & "H"
FieldForAResult = str(ll_Final_A) & "A"
FieldForLResult = str(ll_Final_L) & "L"

I'm sorry I don't have time to duplicate the problem here, as I'm a bit busy today, but hopefully this will help.
 
More help if possible

Hi Matt

on my form I have

2H
1L
1A

I changed your code to

Dim str_Overall As String

Dim str_H As String
Dim str_A As String
Dim str_L As String

Dim str_Final_H As Integer
Dim str_Final_A As Integer
Dim str_Final_L As Integer

str_Overall = [jan2] & [jan3] & [jan4] & [jan5]

str_H = Replace(str_Overall, "H", "")
str_A = Replace(str_Overall, "A", "")
str_L = Replace(str_Overall, "L", "")

str_Final_H = Len(str_H)
str_Final_A = Len(str_A)
str_Final_L = Len(str_L)

fieldforhresult = str_Final_H & "H"
fieldforaresult = str_Final_A & "A"
fieldforlresult = str_Final_L & "L"

This gives me a result of

2h this is correct
3L (should be 1L)
3A (should be 1A)

Any idea what is wrong?

Thanks
Arwyn
 
Yep *sigh* you're suffering for my stupidity. :(

This section is all wrong
Code:
str_H = Replace(str_Overall, "H", "")
str_A = Replace(str_Overall, "A", "")
str_L = Replace(str_Overall, "L", "")
In each of the above cases, you should be replacing the other letters with a blank (""), not the letter you're searching for e.g. if creating str_H, you want anywhere an 'A' or an 'L' appear to be removed, not - as I wrote initially - anywhere an 'H' appears.

Consequently, you're seeing the number of all characters apart from H, as opposed to the number that are H.

This should fix it
Code:
str_H = Replace(str_Overall, "A","") ' --- Removes all As
str_H = Replace(str_H, "L", "")        ' --- Removes all Ls

str_L = Replace(str_Overall, "A","") ' --- Removes all As
str_L = Replace(str_L, "H", "")        ' --- Removes all Hs

str_A = Replace(str_Overall, "H","") ' --- Removes all Hs
str_A = Replace(str_A, "L", "")        ' --- Removes all Ls

I think your life would have been easier if I'd just stayed out of it. :o
 
Success

Dear Matt

The last code works perfectly.

Thank you for your time and effort.

Thank you
Arwyn
 
Str_Final_H = Len(str_H) & "H"

what this is doing is returing the lebgth of the string of H's {len(str_H)} and appending to it the letter H

the error is because you are trying to add a letter to a number.

so to convert something to a string you want cstr so its probably

Str_Final_H = cstr(Len(str_H)) & "H"
 
Hi Gemma

Thanks for your help but it did not work.

Please look at my new message for my next problem.

Thanks
Arwyn
 

Users who are viewing this thread

Back
Top Bottom