473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Yet another VB form to Excel transfer question

14
I have this form built in VB 2010 that mirrors an Excel spreadsheet. I've been at this for a month and I can't get this to work. I need the user to run the program and fill out the form throughout the day. Then I need them to save it to a corresponding date in the workbook with the fields having their data populated and transferred from the form.

I can't for the life of me get this to work and I am running out of options.

The workbook already has formulas in place to do some calculating, but I can't get the data over from the form.

Here it is. The workbook is inside the zip file. Anyone want to take a crack at it?
Attached Files
File Type: zip Daily Log.zip (2.35 MB, 172 views)
Aug 12 '10 #1

✓ answered by Stewart Ross

Hi RobT. Your daily log application would suit direct design within Access, for instance, if you can identify and define the base tables and their relationships involved. You'd save a lot of time and effort, but that's for later.

I show some example VBA code (not VB) below which transfers the contents of two controls from an Access form to an existing Excel worksheet. It's what I had to hand at present and does not relate to the log application you have, but the principles of accessing values in a form and copying them across to Excel are identical.

What you will need to be clear about for yourself is how to loop through the records in your log and transfer the contents of the records to your Excel sheet. This is something that if you were using an ADO recordset, say, as your recordsource for the log table you could do automatically, as there is a range method in Excel called CopyFromRecordSet which will copy all rows of the data from a given recordset as a single operation. If you are to do this yourself then you'd need to loop one row at a time through all the controls on your source form and copy them to Excel accordingly.

The Cells() object I am using in the sample code is a way of referring to individual cells within a worksheet without using the A1, B2 etc notation. This lends itself to processing within loops, where instead of trying to refer to B4, B5 and so on you just set up loop counters as row and column long int numbers, then refer to the rows and columns within the loops using their numeric offsets. Cell(1, 1) is A1, Cell(2, 1) is A2, Cell(1, 2) is B1, and so on.

You need to include a reference to the Excel object library in your project, otherwise you will not be able to use Excel objects and methods at all.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExcel_Click()
  2.     Dim objExcel As Excel.Application
  3.     Set objExcel = New Excel.Application
  4.     With objExcel
  5.         .Visible = True
  6.         .Workbooks.Open ("C:\test.xls")
  7.         With .ActiveSheet
  8.             .Cells(1, 1) = "Date of Birth"
  9.             .Cells(1, 2) = "Anniversary"
  10.             .Cells(2, 1) = Me.DOB
  11.             .Cells(2, 2) = Me.Anniversary
  12.         End With
  13.         .ActiveWorkbook.Save
  14.         .Quit
  15.     End With
  16.     Set objExcel = Nothing
  17. End Sub
-Stewart

4 3440
Stewart Ross
2,545 Expert Mod 2GB
Hi RobT. We will always try to help if you could post something specific for us - an excerpt from a code module which is not working as you would expect, say.

We can't help you if you simply post a VB project - there is no way that contributors can or will wade through it.

Bear in mind that unknown code could pose a security risk to the person downloading it, so asking others to download an unknown project on trust is just not going to get you anywhere.

-Stewart
Aug 14 '10 #2
RobT
14
Thanks Stewart- you've made some valid points and I respect what you said. With that, I have a text box that will have data put in. I have a spreadsheet- and I've been pulling my hair out trying to get data from the text box to a cell in the spreadsheet.

Any sample code? I don't have any code because nothing is working. It seems there are so many different ways to do the same thing, I'm starting to really get confused.

I've included a screenshot of the program, and the spreadsheet to help visualize the project.

For example, the first(row) start time is called Start1. It needs to go in A4. End1 (the second textbox) needs to go in B4. Time1 (the third) needs to go in C4. StartMilesBox needs to go in C30, EndMilesBox in C29, etc etc...

The spreadsheet has the formulas built in, then at the end of the month the workbook gets emailed to the Sergeant. Then he complies it and sends the stats to the Sheriff.

We use the spreadsheet, but it's a pain. I thought building a Daily Log program would be easier for us to compile this stuff- a nicer GUI and a better way to work.

Long term is to get this done, then integrate it with a database, then the Sheriff can compile his stats from the database. I'll deal with that later on though, I just need to get this squared away.




Aug 15 '10 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi RobT. Your daily log application would suit direct design within Access, for instance, if you can identify and define the base tables and their relationships involved. You'd save a lot of time and effort, but that's for later.

I show some example VBA code (not VB) below which transfers the contents of two controls from an Access form to an existing Excel worksheet. It's what I had to hand at present and does not relate to the log application you have, but the principles of accessing values in a form and copying them across to Excel are identical.

What you will need to be clear about for yourself is how to loop through the records in your log and transfer the contents of the records to your Excel sheet. This is something that if you were using an ADO recordset, say, as your recordsource for the log table you could do automatically, as there is a range method in Excel called CopyFromRecordSet which will copy all rows of the data from a given recordset as a single operation. If you are to do this yourself then you'd need to loop one row at a time through all the controls on your source form and copy them to Excel accordingly.

The Cells() object I am using in the sample code is a way of referring to individual cells within a worksheet without using the A1, B2 etc notation. This lends itself to processing within loops, where instead of trying to refer to B4, B5 and so on you just set up loop counters as row and column long int numbers, then refer to the rows and columns within the loops using their numeric offsets. Cell(1, 1) is A1, Cell(2, 1) is A2, Cell(1, 2) is B1, and so on.

You need to include a reference to the Excel object library in your project, otherwise you will not be able to use Excel objects and methods at all.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExcel_Click()
  2.     Dim objExcel As Excel.Application
  3.     Set objExcel = New Excel.Application
  4.     With objExcel
  5.         .Visible = True
  6.         .Workbooks.Open ("C:\test.xls")
  7.         With .ActiveSheet
  8.             .Cells(1, 1) = "Date of Birth"
  9.             .Cells(1, 2) = "Anniversary"
  10.             .Cells(2, 1) = Me.DOB
  11.             .Cells(2, 2) = Me.Anniversary
  12.         End With
  13.         .ActiveWorkbook.Save
  14.         .Quit
  15.     End With
  16.     Set objExcel = Nothing
  17. End Sub
-Stewart
Aug 15 '10 #4
RobT
14
Thanks for your reply, your answer makes sense. I'm going to work this out and make it happen.

Thanks again!
Aug 17 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jake | last post by:
Good Day All, Sorry if I am posting in the wrong forum, hopefully someone will be able to push me in the right direction. What I am trying to accomplish is allowing a client to upload an Excel...
12
by: TheOne | last post by:
In Asp.net web form under form tag there is action field that I am point to some other page, and not to same web form. When I run this page it is always pointing to itself. How do I get around...
0
by: David C. Allen | last post by:
I added a crystal report item to my vb.net project and used to wizard to create a report of an Excel file. I also have a reportviewer on my form and am able to view the report just fine. What I...
0
by: BrianDH | last post by:
Hi I know this is the vb.net NG, but this is a vb.net question for I am using vb.net code behind. In my code I have a button click event on my main web form. Private Sub btNew_Click(ByVal...
2
by: Mr. California | last post by:
As a novice VBA programmer, I have a problem with a routine written as a click procedure from a form to open an Excel template, insert some information, print, and (ideally) return control back to...
10
by: scoopthis | last post by:
Hi, I have an application where I transfer an excel file to a table on SQL through an adp file. The excel file does change so I pass the name parameter from a cmd line. The excel file has a specific...
1
by: Nosferatum | last post by:
I tried to combine a script which process a web form and mails the result, and at the same time stores the entered info into a excel-file on the server. But I just can't make it work. Does it exist...
0
by: Jason Huang | last post by:
Hi, In my C# 1.1 Windows form project, I would like to export record to Excel 2000 spreadsheet Record.xls. I also need to do some data manipulation in the Record.xls. How do I call the VBScript...
0
by: Jason Huang | last post by:
Hi, In my C# 1.1 Windows form project, I would like to export record to Excel 2000 spreadsheet Record.xls. I also need to do some data manipulation in the Record.xls. How do I call the VBA from...
1
by: zeeta999 | last post by:
I want to output the results from an access query to an existing excel worksheet within a workbook. I do not want to delete the worksheet as another worksheet is tied in with formulas. I have no idea...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.