Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 28th, 2008, 11:13 AM
Authorized User
 
Join Date: Feb 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to bonkbc Send a message via Yahoo to bonkbc
Default VB 6.0, use form to write to excel (textbox)

Hello everyone,

I've been working unsuccessfully on this issue all week, and I've finally raised the white flag...I need some help.

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 close to completing this on my own, here is my code:

Private Sub cmdwrite_Click()

Dim oExcel As Excel.Application
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet

Set oExcel = New Excel.Application
oExcel.Visible = True

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


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

Set oRng1 = oWS.Range("A1")

[u]oRng1.Value = Val(txtwrite.Text)</u>

oWB.SaveAs ("writeit.xls")

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 a string value entered into the textbox is lost somewhere along the way upon clicking on the command button. The value written to excel is just "0" (the number zero). However, if I enter a number into the textbox, it works just fine. It seems like a data type issue, but I'm not versed enough in VB to know how to reconcile it.

Another strange finding: if I "hard code" a string, instead of using a text box value (see line of code underlined, bold face above), that also works perfectly. This raises doubts that the problem is a data type issue.

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

-bri


 
Old February 28th, 2008, 12:59 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Replace

oRng1.Value = Val(txtwrite.Text)

with

oRng1.Value = txtwrite.Text

the Val function returns the numbers contained in a string as a numeric value of appropriate type. If your textbox entry is not numeric, it will return zero

Cheers
Shasur


http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old March 3rd, 2008, 11:44 AM
Authorized User
 
Join Date: Feb 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to bonkbc Send a message via Yahoo to bonkbc
Default

thank you thank you thank you thank you! I can't tell you how much time I wasted unsuccesfully journeying through the internet searching for the answer to my problems!

It worked!






Similar Threads
Thread Thread Starter Forum Replies Last Post
excel write to web java applet form mlov83 Excel VBA 0 April 30th, 2008 12:26 PM
Popup cannot write int parentwindow textbox greatJ .NET Framework 2.0 0 August 13th, 2007 02:35 AM
write the values of textbox to a file sandhyamn Pro JSP 1 May 23rd, 2007 10:04 AM
hi help me to write javascript to validate textbox karthikc85 XML 1 October 12th, 2006 03:12 AM
How to write value of checked list in textbox gilgalbiblewheel Classic ASP Databases 2 August 10th, 2005 06:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.