Writing to Excel Using Visual Basic 6.0 form (textbox)-HELP!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonkbc
    New Member
    • Feb 2008
    • 4

    Writing to Excel Using Visual Basic 6.0 form (textbox)-HELP!!

    hi!


    how can we take textbox data from a visual basic 6.0 form and write it to an excel worksheet once a command button is clicked?

    I've gotten very close to completing this on my own, here is my code:

    Private Sub cmdwrite_Click( )

    Dim oExcel As Excel.Applicati on
    Dim oWB As Excel.Workbook
    Dim oWS As Excel.Worksheet

    Set oExcel = New Excel.Applicati on
    oExcel.Visible = True

    Dim oRng1 As Excel.Range
    Dim oRng2 As Excel.Range


    Set oWB = oExcel.Workbook s.Add
    Set oWS = oWB.Worksheets( "Sheet1")

    Set oRng1 = oWS.Range("A1")

    oRng1.Value = Val(txtwrite.Te xt)

    oWB.SaveAs ("writeit.xl s")

    Cleanup:
    Set oWS = Nothing
    If Not oWB Is Nothing Then oWB.Close
    Set oWB = Nothing
    oExcel.Quit
    Set oExcel = Nothing
    End Sub


    Private Sub cmdquit_click()

    End

    End Sub

    the problem I am having, is that the value entered in the textbox is lost somewhere along the way, and the value written to excel is just "0" (the number zero).

    what is happening there?? I'm kind of lost, and would appreciate any help at all!!! thank you very much in advance.

    -bri
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by bonkbc
    hi!


    the problem I am having, is that the value entered in the textbox is lost somewhere along the way, and the value written to excel is just "0" (the number zero).

    what is happening there?? I'm kind of lost, and would appreciate any help at all!!! thank you very much in advance.

    -bri
    Well, i think it's because you're using Val(textbox.tex t). If for some reason the text in the textbox doesn't start with a number, its value will be zero.
    e.g.
    Val("h33") is zero
    Val("33a234") is 33

    HTH

    Comment

    • bonkbc
      New Member
      • Feb 2008
      • 4

      #3
      Originally posted by kadghar
      Well, i think it's because you're using Val(textbox.tex t). If for some reason the text in the textbox doesn't start with a number, its value will be zero.
      e.g.
      Val("h33") is zero
      Val("33a234") is 33

      HTH
      OK! Thanks for the reply! : )

      I guess it is safe to assume that the Val( ) function is only for numbers then? Is there a more generic function I can use that would work regardless if it is a number of a string of characters?

      I appreciate your help,
      Brian

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by bonkbc
        OK! Thanks for the reply! : )

        I guess it is safe to assume that the Val( ) function is only for numbers then? Is there a more generic function I can use that would work regardless if it is a number of a string of characters?

        I appreciate your help,
        Brian
        hmm, what do you want it for?. If you want it to show the variable, just write it, without any function. Val works with both, strings and numbers, but if your string does not start with a number, its value will be zero.

        Comment

        • bonkbc
          New Member
          • Feb 2008
          • 4

          #5
          sorry, nevermind.

          I had a typo syntax error and it's working perfectly. Thanks again for your help!

          -b

          Comment

          Working...