Trying to insert a date within a string in a textbox


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Trying to insert a date within a string in a textbox

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Edmonton, Alberta, Canada
    Posts
    30

    Trying to insert a date within a string in a textbox

    I am trying to insert a date within a string in a textbox, such as:

    “Mary had a little 23-Jul-06 lamb, its fleece was white as snow. 23-Aug-06”.

    Every time I try to insert the date it clears the text from the textbox. I’ve tried everything I can think of, but I’m unable to insert it without clearing the text. The DATE function seems to have a built in ‘clear’ function so each time its called it clears the textbox. Is there a way of disabling this phantom ‘clear’ function? See my coding attempts below:

    Private Sub cmdDate_Click()
    I've tried the following:
    1. Text1.Text = Date

    2. dim date as string
    text1.text = date

    3. text1.text = str(date)

    4. text1.text = Format(Now, "dd mmmm yyyy")
    End Sub

    None of them do what I want. It seems to me that using the STR() function should convert the numeric date to a string that could then be inserted. I don’t know whether there is a textbox property I should be setting at design time that would eliminate the “clearing” effect of the Date function. Can anyone help me? I thank any of you in advance for helping me.

    Urbaud

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Try this:

    Text1.Text = "Mary had a little lamb, its fleece was white as snow."
    ' Set insertion point
    Text1.SelStart = 18
    Text1.SelLength = 0
    ' Insert string into existing text
    Text1.SelText = Format(Now, "dd mmmm yyyy") & " "
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Nov 2005
    Location
    Edmonton, Alberta, Canada
    Posts
    30

    Insert the date several times in a textbox

    Hi Phil,

    Thank you for replying to my post. I tried your suggested code and it works. However, I realize that I didn’t make myself clear in the post. I’m talking about large bocks of text, not a single line of text. I have a program and the textbox in it is set to multiline. When I see a block of text that I want to keep, I just paste it into the text box.

    In the past I have had to type the date but that gets tiresome, so I thought I’d create a DATE command button which would allow me to place the cursor anywhere in the text, then click the Date button and presto, the date is inserted. Each time I try new code (as cited above), none of it works and when the date is inserted it clears all of the text. With your code, you were able to choose initially (at design time) where to place the date, the 18th character position. I don’t know at design time where I will be placing the date or dates, so that’s why I need to be able to just place the cursor any place and click the DATE button. If I have a large body of text I may want to insert the date at several places throughout the text.

    Is there a way of turning the date into a string that will fit into the text without clearing it? I thought the STR() function would do it, but obviously not. I sure hope this reply is clearer in terms of what I want to do. Any suggestions would be most appreciated. Thanks again for replying.

    Dan (urbaud)

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Hi, Dan: Turning the date into a string is no problem:

    Dim sDate As String
    sDate = Format(Now, "dd mmmm yyyy")

    It's not converting the date to a string that clearing your textbox, it's assigning the textbox's .Text property. You're saying, in effect, "Throw away the current text in the textbox and replace it with this."

    If you want to click within the text to set the insertion point, just skip the line from my previous post that sets the SelStart. When you assign the SelText, it will insert it at the cursor's location.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Nov 2005
    Location
    Edmonton, Alberta, Canada
    Posts
    30
    Hi Phil,

    Your amazing! The code works and it's so simple, basically two lines of code. Thank you so much.
    Dan (urbaud)

Similar Threads

  1. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 22
    Last Post: 12-03-2001, 03:53 AM
  2. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 06:24 PM
  3. For any JDBC: wrap Date inside String
    By Fabio Luis De Paoli in forum Java
    Replies: 0
    Last Post: 03-03-2001, 01:12 PM
  4. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  5. Multi-row calculations
    By Bob Hines in forum Database
    Replies: 7
    Last Post: 04-27-2000, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center