PDA

View Full Version : can anybody help me with this?



miketallica
09-10-2009, 12:09 PM
i need the 2 letters after the date to change automatically to the correct 2 letters.

example;

today is the 1st day of august

or

today is the 3rd day of august

it needs to change based on what day it is.

fumei
09-10-2009, 01:47 PM
1. using
.FormFields("Text1").Range.Text = TextBox2.Value
is, while not technically wrong, is not proper use of the object. You are trying put - it seems - the text from the userform textboxes at the location of the formfields...BUT, you do not actually use the formfields. In which case...why have them? The correct syntax for getting into formfields is:
.FormFields("Text1").Result = TextBox2.Value
Further, while again not technically wrong, it is a best practice/good idea to use explicit terms.

.FormFields("Text1").Result = TextBox2.Text
Yes, the default .Value for a textbox is, in fact, the text. That is why it is not "wrong". However, by not using what you actually want - the text - at some point you may be surprised by what you get from .Value of a control. It may not be what you assume it to be.

As it is, when you put the text value into the range of the formfield, the text indeed goes there, but the formfield is deleted. Is this what you want to happen? Again, if so...why have the formfields in the first place? As it stands this can only be used ONCE, as the formfields get deleted, and therefore the code is useless to use again.

2. Why are you doing this? it seems odd.

3. You are not using Option Explicit in your modules. I VERY STRONGLY suggest you start to do so. Immediately.

4. I understand this is likely a developmental effort, but get into the habit of naming things well. The order of formfields in your document are: Text2, Text1, Text3. Not only are they names rather meaningless, but they can not even be looped through in any meaningful way.

5. You could gain a great deal by walking through the order of events, as fired by a userform. As it is, you are using Sub TextBox2_Enter() to put "September" the appropriately named Textbox2. Month may be a better name....

OK, that is fine. Except, I change it to "October - and BTW, having to type the months is a decidely non-userfriendly design - and I change the day (the next textbox, and go...ah, crap, I mean "November". Because you are using Sub TextBox2_Enter() EVERY TIME you enter the textbox, it becomes (in this case) "September". Always. In other words, even though I changed it to "October", and I want to change it to "November"...your code changes it to "September" first. That is annoying.

6. Yes, this can be done quite easily. I would remove your hard coded "st", and simply truncate the day value (1, 2, 3, 4....) with the appropriate suffix using a Select Case logic statement. Because it IS logic.

1 = 1st
etc.
etc.
18 = 18th
etc.
etc.
21 = 21st

7. Since this is called "Current Date" why do you have user input at all? Just put in the current date in the format you choose. However, I think you actually do want user input, but again, if it is a one-time thing, why bother with deleting formfields? if it is NOT a one-time thing, then a redesign may be in order.

8. regarding the logic of the suffix, you of course have to be wary of the oddness of English at times.

3 = 3rd
13 = 13th
23 = 23rd

So the fact the number you are working with ends with 3 does NOT mean the suffix is "rd". There is logic, and it can be done.

Take at look at the attached file back.

miketallica
09-10-2009, 02:08 PM
what i have is template i use all the time. before we were deleting and retyping the information. so i decided to try and write the code to populate the entire document. i am very new to vba and as of last week am taking a vba class at the local community college. i did notice that the text boxes disappeared after the form was filled out which i did not want but since i would be generating a new form every time with this template it wasnt a big deal. the date populates in several places in the document like this September 3, 2009. but the first sentence in the doc says "On this 3rd day of September blah blah blah. so that is the reason i am looking to have those last to letters change automatically if possible. that way when i open the doc, fill out the form it is ready for printing with out any further editing. as far as the date goes it will always be the current date. i did this the only way i know how by googleing and asking questions and so far for a novice i am pretty excited about what i have a accomplished with vba and i do realize there are rules and formalities to writting vba code and obviously that will come over time and with the help of this class. any chance you would be willing to share that code with me so i can pick apart how you did it and learn? ~Mike

fumei
09-10-2009, 02:17 PM
Ah, so this is a homework kind of thing. Sorry, but we have a rule not to help with homework.

That being said, IF you ask the right questions, AND you show that you have actually worked on something, yes, we can help to a certain extent.

So.......

I have already told you (or at least hinted) that to get a value of, say, 1 (from your userform) and make that 1st, you can use Select Case.

Start there. Show me what you can come up with as code to do only that. It is purely logic - that is...THINKING. The coding part is quite secondary.

Look up what Select case does, and code something that will return the proper ending, no matter what the input.

If you give it 1, it gives you 1st; if you give 23, it gives you 23rd, if you give it 17, it gives you 17th; etc. etc.

This is basic logic (thinking) structure, and you MUST work on those basics to really move forward.

miketallica
09-10-2009, 02:40 PM
it's actually not a homework assignment at all. i have been messing around with vba on and off for a few months now. making message boxes pop up when you open a document and say something like "hey " & username and i would mess with some of the guys at work with that stuff but trying to wrap my head around vba understanding the lingo and all that has been tuff trying to learn my self. i havent tried anything more until now i have recently changed jobby's and my new boss comes up to me and says " is there a way we can make this document easier to fill out?" and obviously i am excited to do it because now i have a vba project to practice on. i found a link online that helped me get to this point. they provided a document that i was able to pick apart to see how it was done. honestly that is how i personally learn. i am absolutely a hands on person. but when i get stuck i google, i ask questions in forums and hopefully i find my answer. i even asked the teacher yesterday on how to do it and offered to show her my code and form which she declined. she just sat there and told me how to do it in vba lingo. i have no idea what the f*** she was talking about. i do know what i want and i know it can be done but i need to see it.

macropod
09-10-2009, 03:06 PM
Hi Miketallica,

Since, it seems, all you need is for the current date to appear at the appropriate places and in the appropriate format in the Word document, you should be aware that you can do this without resorting to vba at all.

To see how to do this and just about everything else you might want to do with dates in Word, check out my Word Date Calculation Tutorial, at:
http://www.wopr.com/index.php?showtopic=249902 (mhtml:{2603C627-B617-484B-B3E3-521BCFB808E8}mid://00000006/!x-usc:http://www.wopr.com/index.php?showtopic=249902)
or
http://www.gmayor.com/downloads.htm#Third_party (mhtml:{2603C627-B617-484B-B3E3-521BCFB808E8}mid://00000006/!x-usc:http://www.gmayor.com/downloads.htm#Third_party)
Do read the document's introductory material.

fumei
09-11-2009, 12:23 PM
I agree, and I sort of asked that already.


7. Since this is called "Current Date" why do you have user input at all? Just put in the current date in the format you choose.
macropod is quite correct, if what you want is the current date...use a field, not VBA.

You are not consistent. You say this is not homework, but you keep refering to a teacher and a course.

"i do know what i want and i know it can be done but i need to see it."

No...you need to try it doing it. As I stated, try and post what code you come up with. I am not going to give you code. I am trying to give you help.

"i am absolutely a hands on person. "

Then...again, try doing something hands on and posting back. You did do something, and it was a decent effort. Try some more. My example document shows a possible route. I gave some, hopefully, positive comments on what you should be trying.

Please do not post anything like: " i have no idea what the f*** she was talking about." Comments like that will shut us down in terms of helpful you.

If you do not understand something she said, do more research. Use Help, it is much much better than it used to be. Learn to use the Object Browser.

More importantly, learn to articulate "i do know what i want" into very precise, explicit, logic.

So far, you have not done this.

"she just sat there and told me how to do it in vba lingo." Perhaps. Then learn the lingo. The best way to start is - again - be very very very very clear and explicit about EXACTLY what you want to do, right down to the smallest detail you can think of.

IMO, 90% of good VBA coding is thinking clearly.

I will repeat myself. Figure out (if indeed you are going the VBA route, and not a field as macropod suggests), how to take ANY input (1, 23, 30, 12, 2, 413) and:

1. determine if it is a valid input for a date (413 obviously is not);

2. turn that input (1, 23, 30, 12, 2) into the appropriate result.

It is not actually very hard at all. You do not need any advanced VBA "lingo". But you do need to think clearly, and explicitly.

In my example the code (a Select Case) takes 14 lines (it could be less but I like to use neat lines indented and broken up for easy reading). That is it. It can take ANY input and add the appropriate "st", "rd", "th" after it.

N.B. it is a bit longer in my own version as I put in strong error-trapping. It rejects any input like "sdgsgd" (strings that are obviously not numeric); 413 (there can only be up to two characters...); and any two character/numeric > 31 (32nd Day is not valid, now is it?). To be fully professional, there would ALSO be error trapping against the Month. You can not have:

Today is the 30th Day of February in year 2009.

now can you? YOUR code would allow that.

And getting those possible errors trapped has NOTHING - repeat nothing - to do with understanding VBA "lingo".

It has EVERYTHING to do with thinking clearly, explicitly, exactly.

miketallica
09-11-2009, 12:40 PM
this is not a homework assignment. its just a project i started working on to help myself learn vba. the class im taking just started a week ago and i have been working on this prior to that. i know i can do this with form fields but thats not what i want to do. i will never have the problem of the 30th of February because i am always using the current date. as i have said before this is a template. every document generated with this template gets printed out and deleted. i'll try and find somebody else to help me. thanks though.

lucas
09-11-2009, 01:32 PM
Well, Mike, you can do that or you can research select case and post any specific questions back here instead of trying to get someone to just do it for you.

If it's a project and not homework but the intent is still to learn as you have indicated, you will have a hard time finding better help than you will get from this forum. Many here however, are not inclined to just give you a solution, especially after you yourself indicated a desire to learn.

They will help you to learn if you really want it. Gerry(fumei) is an instructor so it seems like you want to tell the teacher how to teach.

In any event, and I think I can safely say that in this I speak for all here at the forum, good luck.

fumei
09-11-2009, 02:12 PM
Mike, if this is true:

"i will never have the problem of the 30th of February because i am always using the current date. "

Then your answer has already been given, and walking away in any sort of snit is counter productive...for YOU.

If you always using the current date, then use a field, as macropod has already suggested. He even provided links to his superb tutorials on using dates fields. These are about the best sources of using fields in Word you are going to find anywhere.

" i'll try and find somebody else to help me. "

You can do all the "trying" you like, but if you want to use fields, then you simply will not find anything better than what macropod has offered.

If you work though those you WILL greatly increase your knowledge and ability. So, you already have the answer, and you do not need to search elsewhere. If you wish to ignore such excellent sources of information...your loss.

That is unless you are actually unwilling to do any work. In which case....whatever.

"i know i can do this with form fields but thats not what i want to do."

Do you now? And how do you "know" that....hmmmmmm?

I will state flatly, based on much experience, that formfields are NOT the way to do what you seem to want to do.

Especially since you are seemingly illiterate about actually saying what you do want to do.

Help you? We are trying to help. If you had post even a tiny little bit of code trying to do what I suggested (the Select Case), I would have helped you correct any problems you had with it.

But you did nothing. If by "find somebody else to help me" you mean give you something (working code) on a silver platter...I am not your Help Desk.

If you had even tried a wee bit, any thing, I would have helped you make it work and/or improve it. As it is, with your responses (and lack thereof)....not a chance. You have thrown away help you could have received.

Paul_Hossler
09-13-2009, 09:33 AM
this is not a homework assignment. its just a project i started working on to help myself learn vba.

In addition to the other suggestions, and staying within the bounds that you've established for the way you want to do it, I'd suggest

1. putting the code into different types of modules (standard, userform)
2. naming the controls with more meaningful names (e.g. tbDay instead of TextBox2, etc.)


In a standard module --


Option Explicit
Public Sub Ending()
Application.ScreenUpdating = False
Load UserForm1
UserForm1.Show
Application.ScreenUpdating = True
End Sub



and in the UserForm module


Option Explicit

Private Sub UserForm_Initialize()
With Me
.TextBox3.Value = Day(Now())
.TextBox2.Value = Format(Now(), "mmmm")
.TextBox4.Value = Year(Now())
End With
End Sub

Private Sub CommandButton1_Click()
With ActiveDocument
.FormFields("Text1").Range.Text = Me.TextBox2.Value
Select Case CLng(Me.TextBox3.Value)
Case 1, 21, 31
.FormFields("Text2").Range.Text = Me.TextBox3.Value & "st"
Case 2, 22,
.FormFields("Text2").Range.Text = Me.TextBox3.Value & "nd"
Case 3, 23
.FormFields("Text2").Range.Text = Me.TextBox3.Value & "rd"
Case Else
.FormFields("Text2").Range.Text = Me.TextBox3.Value & "th"
End Select
.FormFields("Text3").Range.Text = Me.TextBox4.Value
End With

Me.Hide
Unload Me
End Sub


I believe that the more effecient and 'Word-oriented' ways others suggested are valid and more appropriate, but maybe after you've got this bit of VBA working, you'll re-consider them.

Paul