Question

Format Excel or CSV Header and Do Error Check VBScript

Asked by: lbarnett419

2 issues with VBScript:
(1) The outputfile is csv but I'd prefer xls, if possible. Would like to format header with bold and when Category changes, add a row in the file to separate the Cateogories
(2) The script halts when it gets to the "Set file = fs.OpenTextFile......" with a 'Path not Found' with 4 Schoolnums. I checked the two data files and can't find the problem..as they both have the SCHOOLNUM. Until I find the error,  can I run the script through by adding a line that runs past that error or will I have to do a SCHOOLNUM not in (....)?
Thank you.

Set fs = CreateObject("Scripting.FileSystemObject")
' Set constants here___________________________________________'
myDelimeter = ","
myTextQualifier = """"	' Will put one double quote on each side of the field
myspace = "    "
myCurFilePath = "C:\Test\"	'Path where vbs script is located
strsource1 = "\\Server1\apps\Final\FINALReports\"	
myOutPutFileName = "ExtractResults.csv"'FileName of the import file
 
OdbcDSN = "DSN=Server5;UID=xxx;PWD=xxx" 
Set connect = CreateObject("ADODB.Connection")
connect.Open OdbcDSN
 
OdbcDSN2 = "DSN=Server5;UID=xxx;PWD=xxx" 
Set connect2 = CreateObject("ADODB.Connection")
 
'Select schools to extract___________________________________________'
SQL ="Select SCHOOLNUM,SchoolNameDistrict,Type from "
SQL = SQL & "SchoolErrorRecipients "
 
Set resultSet = connect.Execute(SQL)
If Not resultset.eof Then
	resultSet.MoveFirst
End If
tmpcount = 0
 
' LOOP FOR EACH SCHOOL RETURNED FROM SERVER INFO______'
Do While Not resultset.EOF
    connect2.Open OdbcDSN2
         o_Schoolnum= resultset("SCHOOLNUM")
         o_SchoolNameDistrict = resultset("SchoolNameDistrict")
         o_Type = resultset("Type")
 
'DELETE EXISTING FOLDERS
If fs.FolderExists(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict) then
fs.DeleteFolder(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict)
End If
 
'CREATE FINALReports FOLDERS
	If fs.FolderExists(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict) then
		' Do Nothing
        Else
         fs.CreateFolder(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict)
        End If
    
	Set file = fs.OpenTextFile(strsource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict & "\" & myOutPutFileName, 2, True) 
        file.WriteLine """SCHLNAME"",""CATEGORY"",""Messages"",""ERRORS"""
'Get Student Information for current school
SQL1 = "select SchoolNameDistrict,Category,Messages,Errors "
SQL1 = SQL1 & "from "
SQL1 = SQL1 & "FinalMasterExtract a, ErrorRecipients b "
SQL1 = SQL1 & "where a.SCHOOLNUM=b.SCHOOLNUM and b.SCHOOLNUM = "& o_Schoolnum &" " 
 
Set resultSet2 = connect2.Execute(SQL1)
If Not resultset2.eof Then
	resultSet2.MoveFirst
End If
 
tmpcount2 = 0
'INNER LOOP FOR EACH STUDENT AT CURRENT SCHOOL
  Do While Not resultset2.eof
            o_SchlName   = resultset2("SchoolNameDistrict")
            o_Category   = resultset2("Category")
	   o_Messages   = resultset2("Messages")
            o_Errors     = resultset2("Errors")
   
' Write Student Info to File'
'Use following 2 lines to put double quotes around each field
	  file.Write myTextQualifier & o_SchlName  & myTextQualifier & myDelimeter _  
                 & myTextQualifier & o_Category  & myTextQualifier & myDelimeter _
                 & myTextQualifier & o_Messages  & myTextQualifier & myDelimeter _  
                 & myTextQualifier & o_Errors    & myTextQualifier & myDelimeter & vbcrlf  
    
       
		resultset2.MoveNext
		tmpcount2 = tmpcount2 + 1
	Loop
	file.Close	
			
	resultset.MoveNext
  tmpcount = tmpcount + 1
  connect2.Close
Loop
 
connect.Close
Set connect = Nothing

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-09-23 at 09:59:49ID23755679
Topics

VB Script

,

SQL Server 2005

Participating Experts
1
Points
500
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Setting answer to bold in a vbscript.
    <%@LANGUAGE="VBSCRIPT"%> <% dim varEmail, varContent varEmail = request("email") varContent ="Name of Requester: " & request("Requester") & vbcrlf & "" & "Site: " & request("Site&q...
  2. Importing CSV file into Exel using VBscript
    Users in my company receive periodic csv files from which I need to extract some particular information. The problem is that these files (may be created in UNIX) when opened in MS Exel have lots of ";" as delimiters for the colomns and only by using the import proc...
  3. Converting a .CSV format to a 97-2003 .XLS format
    Is there a VB script where i can take a .csv file and convert it to a MS Excel 97-2003 .xls format? I want to automate the process of opening a .csv file in Excel, doing the save as, selecting "Microsoft Excel 97-Excel 2003 &5.0/95 Workbook (*.xls)" and saving....
  4. problem with dates wen saving an xls file to csv using a vb…
    Hi I have a vbscript which opens an xls file and saves it as a csv. I can do this fine but the problem i am having is that the dates in the xls file eg "29/08/2005" are being converted to "8/29/2005". the line i am using in my vbscript to save as csv is ...
  5. VBScript, XLS, Formattting and other Functions
    Hello, does anyone have a list of properties that you can change when writing a XLS file from VBScripting? Thing I am interested... How to write to multiple sheets in the same file... Say a new page per server, but in the same xls. How to change the cell colors How to chan...
  6. vbScript
    Hi how can i get a systems variables values in a vbscript I know Visual Basic, but i,m new to vbscript

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: RobSampsonPosted on 2008-09-23 at 23:51:17ID: 22557339

Hi, for requirement number 1, the easiest solution would probably be to programatically open each CSV after it's finished writing, then do a "Save As", and then do the formatting.  The below code will do that.

For requirement number 2, the code below should address that too, at least by catching the error and telling you what it tried to open.

Regards,

Rob.

Set fs = CreateObject("Scripting.FileSystemObject")
' Set constants here___________________________________________'
myDelimeter = ","
myTextQualifier = """"	' Will put one double quote on each side of the field
myspace = "    "
myCurFilePath = "C:\Test\"	'Path where vbs script is located
strsource1 = "\\Server1\apps\Final\FINALReports\"	
myOutPutFileName = "ExtractResults.csv"'FileName of the import file
 
Const xlNormal = -4143
 
OdbcDSN = "DSN=Server5;UID=xxx;PWD=xxx" 
Set connect = CreateObject("ADODB.Connection")
connect.Open OdbcDSN
 
OdbcDSN2 = "DSN=Server5;UID=xxx;PWD=xxx" 
Set connect2 = CreateObject("ADODB.Connection")
 
'Select schools to extract___________________________________________'
SQL ="Select SCHOOLNUM,SchoolNameDistrict,Type from "
SQL = SQL & "SchoolErrorRecipients "
 
Set resultSet = connect.Execute(SQL)
If Not resultset.eof Then
	resultSet.MoveFirst
End If
tmpcount = 0
 
' LOOP FOR EACH SCHOOL RETURNED FROM SERVER INFO______'
Do While Not resultset.EOF
	connect2.Open OdbcDSN2
	o_Schoolnum= resultset("SCHOOLNUM")
	o_SchoolNameDistrict = resultset("SchoolNameDistrict")
	o_Type = resultset("Type")
 
	'DELETE EXISTING FOLDERS
	If fs.FolderExists(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict) Then
		fs.DeleteFolder(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict)
	End If
	 
	'CREATE FINALReports FOLDERS
	If fs.FolderExists(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict) then
		' Do Nothing
	Else
		fs.CreateFolder(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict)
	End If
	    
	strCSVFileName = strsource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict & "\" & myOutPutFileName
	On Error Resume Next
	Set file = fs.OpenTextFile(strCSVFileName, 2, True)
	If Err.Number <> 0 Then
		MsgBox "Failed to create " & strCSVFileName
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0
		file.WriteLine """SCHLNAME"",""CATEGORY"",""Messages"",""ERRORS"""
		'Get Student Information for current school
		SQL1 = "select SchoolNameDistrict,Category,Messages,Errors "
		SQL1 = SQL1 & "from "
		SQL1 = SQL1 & "FinalMasterExtract a, ErrorRecipients b "
		SQL1 = SQL1 & "where a.SCHOOLNUM=b.SCHOOLNUM and b.SCHOOLNUM = "& o_Schoolnum &" " 
		 
		Set resultSet2 = connect2.Execute(SQL1)
		If Not resultset2.eof Then
			resultSet2.MoveFirst
		End If
		 
		tmpcount2 = 0
		'INNER LOOP FOR EACH STUDENT AT CURRENT SCHOOL
		Do While Not resultset2.eof
			o_SchlName   = resultset2("SchoolNameDistrict")
			o_Category   = resultset2("Category")
			o_Messages   = resultset2("Messages")
			o_Errors     = resultset2("Errors")
		   
			' Write Student Info to File'
			'Use following 2 lines to put double quotes around each field
			file.Write myTextQualifier & o_SchlName  & myTextQualifier & myDelimeter _  
				& myTextQualifier & o_Category  & myTextQualifier & myDelimeter _
				& myTextQualifier & o_Messages  & myTextQualifier & myDelimeter _  
				& myTextQualifier & o_Errors    & myTextQualifier & myDelimeter & vbcrlf  
			resultset2.MoveNext
			tmpcount2 = tmpcount2 + 1
		Loop
		file.Close	
	
		strExcelFileName = Left(strCSVFileName, InStrRev(strCSVFileName, ".") - 1) & ".xls"
		Set objExcel = CreateObject("Excel.Application")
		Set objWB = objExcel.workbooks.open(strCSVFileName, False, False)
		objWB.Rows("1:1").Font.Bold = True
		objWB.SaveAs strExcelFileName, xlNormal
		objWB.Close False
		objExcel.Quit
	End If
 
	resultset.MoveNext
	tmpcount = tmpcount + 1
	connect2.Close
Loop
 
connect.Close
Set connect = Nothing

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:

Select allOpen in new window

 

by: lbarnett419Posted on 2008-09-24 at 05:18:51ID: 22558850

ok, I'll try test it out! Thanks!

 

by: lbarnett419Posted on 2008-09-24 at 07:57:20ID: 22560368

Rob, thank you ---working so far, except for a couple of problems:
(1) First error: At objWB.Rows..., Object doesn't support this property or method:'objWB.Rows'
(2) Looks like the folders and files are created and then the error is Incorrect syntax near '='  at the Set resultSet2 = connect2.Execute(SQL1) line...maybe I missed something..what do you think I should be looking for?
(3) Not an error, but can I format the length of the columns with a line?

 

by: RobSampsonPosted on 2008-09-24 at 16:17:51ID: 22565004

Hi, I don't actually think you need connect2 and ODBCDSN2.  I'm pretty sure you can just get away with creating RecordSet and RecordSet2 from the same Connect object.

Try this.  I have also fixed the Rows error, by creating a reference to the first sheet (you can change objWB.Sheets(1) in the code to reference another sheet if required).  I have also added an "AutoFit" command to automatically expand the column width to fit.

Regards,

Rob.

Set fs = CreateObject("Scripting.FileSystemObject")
' Set constants here___________________________________________'
myDelimeter = ","
myTextQualifier = """"	' Will put one double quote on each side of the field
myspace = "    "
myCurFilePath = "C:\Test\"	'Path where vbs script is located
strsource1 = "\\Server1\apps\Final\FINALReports\"	
myOutPutFileName = "ExtractResults.csv"'FileName of the import file
 
Const xlNormal = -4143
 
OdbcDSN = "DSN=Server5;UID=xxx;PWD=xxx" 
Set connect = CreateObject("ADODB.Connection")
connect.Open OdbcDSN
 
'Select schools to extract___________________________________________'
SQL ="Select SCHOOLNUM,SchoolNameDistrict,Type from "
SQL = SQL & "SchoolErrorRecipients "
 
Set resultSet = connect.Execute(SQL)
If Not resultset.eof Then
	resultSet.MoveFirst
End If
tmpcount = 0
 
' LOOP FOR EACH SCHOOL RETURNED FROM SERVER INFO______'
Do While Not resultset.EOF
	o_Schoolnum= resultset("SCHOOLNUM")
	o_SchoolNameDistrict = resultset("SchoolNameDistrict")
	o_Type = resultset("Type")
 
	'DELETE EXISTING FOLDERS
	If fs.FolderExists(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict) Then
		fs.DeleteFolder(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict)
	End If
	 
	'CREATE FINALReports FOLDERS
	If fs.FolderExists(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict) then
		' Do Nothing
	Else
		fs.CreateFolder(strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict)
	End If
	    
	strCSVFileName = strsource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict & "\" & myOutPutFileName
	On Error Resume Next
	Set file = fs.OpenTextFile(strCSVFileName, 2, True)
	If Err.Number <> 0 Then
		MsgBox "Failed to create " & strCSVFileName
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0
		file.WriteLine """SCHLNAME"",""CATEGORY"",""Messages"",""ERRORS"""
		'Get Student Information for current school
		SQL1 = "select SchoolNameDistrict,Category,Messages,Errors "
		SQL1 = SQL1 & "from "
		SQL1 = SQL1 & "FinalMasterExtract a, ErrorRecipients b "
		SQL1 = SQL1 & "where a.SCHOOLNUM=b.SCHOOLNUM and b.SCHOOLNUM = "& o_Schoolnum &" " 
		 
		Set resultSet2 = connect.Execute(SQL1)
		If Not resultset2.eof Then
			resultSet2.MoveFirst
		End If
		 
		tmpcount2 = 0
		'INNER LOOP FOR EACH STUDENT AT CURRENT SCHOOL
		Do While Not resultset2.eof
			o_SchlName   = resultset2("SchoolNameDistrict")
			o_Category   = resultset2("Category")
			o_Messages   = resultset2("Messages")
			o_Errors     = resultset2("Errors")
		   
			' Write Student Info to File'
			'Use following 2 lines to put double quotes around each field
			file.Write myTextQualifier & o_SchlName  & myTextQualifier & myDelimeter _  
				& myTextQualifier & o_Category  & myTextQualifier & myDelimeter _
				& myTextQualifier & o_Messages  & myTextQualifier & myDelimeter _  
				& myTextQualifier & o_Errors    & myTextQualifier & myDelimeter & vbcrlf  
			resultset2.MoveNext
			tmpcount2 = tmpcount2 + 1
		Loop
		file.Close	
	
		strExcelFileName = Left(strCSVFileName, InStrRev(strCSVFileName, ".") - 1) & ".xls"
		Set objExcel = CreateObject("Excel.Application")
		Set objWB = objExcel.workbooks.open(strCSVFileName, False, False)
		objExcel.Visible = True
		Set objSheet = objWB.Sheets(1)
		objSheet.Rows("1:1").Font.Bold = True
		objSheet.Columns.AutoFit
		objWB.SaveAs strExcelFileName, xlNormal
		objWB.Close False
		objExcel.Quit
	End If
 
	resultset.MoveNext
	tmpcount = tmpcount + 1
Loop
 
connect.Close
Set connect = Nothing

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:

Select allOpen in new window

 

by: lbarnett419Posted on 2008-09-24 at 16:46:06ID: 22565130

Will try it!

 

by: lbarnett419Posted on 2008-09-24 at 18:16:49ID: 22565455

Rob: Seems like it goes through the entire process ok..the folders and files have been created (with the exception of the 4, but the Error handling took care of that). However, I still get an error pointing to Line 60 -  Incorrect syntax near '='  at the Set resultSet2 = connect.Execute(SQL1).        (??)

 

by: RobSampsonPosted on 2008-09-24 at 18:23:27ID: 22565473

Oh you what?  Maybe that's a SQL error, and not a VBScript error.

SQL1 is this:

            SQL1 = SQL1 & "from "
            SQL1 = SQL1 & "FinalMasterExtract a, ErrorRecipients b "
            SQL1 = SQL1 & "where a.SCHOOLNUM=b.SCHOOLNUM and b.SCHOOLNUM = "& o_Schoolnum &" "

try removing the spaces around the equals sign (and you don't need the blank bit at the end), so it becomes this:

            SQL1 = SQL1 & "from "
            SQL1 = SQL1 & "FinalMasterExtract a, ErrorRecipients b "
            SQL1 = SQL1 & "where a.SCHOOLNUM=b.SCHOOLNUM and b.SCHOOLNUM=" & o_Schoolnum


Regards,

Rob.

 

by: lbarnett419Posted on 2008-09-25 at 07:13:55ID: 22569340

For some reason, I can't get around the error...I closed up every space around all  the equals signs...do you think it could be a problem with the data? The reason I'm asking is that now I'm getting more of the 'Couldn't create the csv file' notifications...

 

by: RobSampsonPosted on 2008-09-25 at 15:47:48ID: 22574763

Hmmm, when you get the Failed to create <filename.csv> message, does the path look correct, and would you be able to manually create that exact path?  There's not two slashes in it or something?

 

by: lbarnett419Posted on 2008-09-26 at 16:59:55ID: 22584899

Yes, the path is correct. The folder is created each time but the csv file, for some of the schoolnums, no longer gets created even though they did previously..one peculiar thing, it isn't creating  any that are less than schoolnum=010. So any with 001,002,003 don't get created anymore.
Been trying one at a time (for example schoolnum='002' )and I know the data is in the file, but no success.

 

by: RobSampsonPosted on 2008-09-27 at 17:31:59ID: 22589273

Hmmmm.. I just noticed that when you're checking for the folder, there's no backslashes between each value.  Should there be?

Anyway, I haven't changed anything functionally, but I've made it a bit more readable.  I've changed the folder path into one variable, so you only have to modify in one place. I've also added some message boxes as it deletes or creates so you can see the path it's referring to.

Let me know if the paths are correct, or if we need to add backslashes or something...

Regards,

Rob.

Set fs = CreateObject("Scripting.FileSystemObject")
' Set constants here___________________________________________'
myDelimeter = ","
myTextQualifier = """"	' Will put one double quote on each side of the field
myspace = "    "
myCurFilePath = "C:\Test\"	'Path where vbs script is located
strsource1 = "\\Server1\apps\Final\FINALReports\"	
myOutPutFileName = "ExtractResults.csv"'FileName of the import file
 
Const xlNormal = -4143
 
OdbcDSN = "DSN=Server5;UID=xxx;PWD=xxx" 
Set connect = CreateObject("ADODB.Connection")
connect.Open OdbcDSN
 
'Select schools to extract___________________________________________'
SQL ="Select SCHOOLNUM,SchoolNameDistrict,Type from "
SQL = SQL & "SchoolErrorRecipients "
 
Set resultSet = connect.Execute(SQL)
If Not resultset.eof Then
	resultSet.MoveFirst
End If
tmpcount = 0
 
' LOOP FOR EACH SCHOOL RETURNED FROM SERVER INFO______'
Do While Not resultset.EOF
	o_Schoolnum= resultset("SCHOOLNUM")
	o_SchoolNameDistrict = resultset("SchoolNameDistrict")
	o_Type = resultset("Type")
 
	'DELETE EXISTING FOLDERS
	strFolder = strSource1 & o_Type & o_Schoolnum & o_SchoolNameDistrict
	If fs.FolderExists(strFolder) Then
		MsgBox strFolder & VbCrLf & "exists.  Deleting folder."
		fs.DeleteFolder(strFolder)
	Else
		MsgBox strFolder & VbCrLf & "does not exist.  Not deleting folder."
	End If
	 
	'CREATE FINALReports FOLDERS
	fs.CreateFolder(strFolder)
	    
	strCSVFileName = strFolder & "\" & myOutPutFileName
	On Error Resume Next
	Set file = fs.OpenTextFile(strCSVFileName, 2, True)
	If Err.Number <> 0 Then
		MsgBox "Failed to create " & strCSVFileName
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0
		file.WriteLine """SCHLNAME"",""CATEGORY"",""Messages"",""ERRORS"""
		'Get Student Information for current school
		SQL1 = "select SchoolNameDistrict,Category,Messages,Errors "
		SQL1 = SQL1 & "from "
		SQL1 = SQL1 & "FinalMasterExtract a, ErrorRecipients b "
		SQL1 = SQL1 & "where a.SCHOOLNUM=b.SCHOOLNUM and b.SCHOOLNUM=" & o_Schoolnum 
		 
		Set resultSet2 = connect.Execute(SQL1)
		If Not resultset2.eof Then
			resultSet2.MoveFirst
		End If
		 
		tmpcount2 = 0
		'INNER LOOP FOR EACH STUDENT AT CURRENT SCHOOL
		Do While Not resultset2.eof
			o_SchlName   = resultset2("SchoolNameDistrict")
			o_Category   = resultset2("Category")
			o_Messages   = resultset2("Messages")
			o_Errors     = resultset2("Errors")
		   
			' Write Student Info to File'
			'Use following 2 lines to put double quotes around each field
			file.Write myTextQualifier & o_SchlName  & myTextQualifier & myDelimeter _  
				& myTextQualifier & o_Category  & myTextQualifier & myDelimeter _
				& myTextQualifier & o_Messages  & myTextQualifier & myDelimeter _  
				& myTextQualifier & o_Errors    & myTextQualifier & myDelimeter & vbcrlf  
			resultset2.MoveNext
			tmpcount2 = tmpcount2 + 1
		Loop
		file.Close	
	
		strExcelFileName = Left(strCSVFileName, InStrRev(strCSVFileName, ".") - 1) & ".xls"
		Set objExcel = CreateObject("Excel.Application")
		Set objWB = objExcel.workbooks.open(strCSVFileName, False, False)
		objExcel.Visible = True
		Set objSheet = objWB.Sheets(1)
		objSheet.Rows("1:1").Font.Bold = True
		objSheet.Columns.AutoFit
		objWB.SaveAs strExcelFileName, xlNormal
		objWB.Close False
		objExcel.Quit
	End If
 
	resultset.MoveNext
	tmpcount = tmpcount + 1
Loop
 
connect.Close
Set connect = Nothing

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:

Select allOpen in new window

 

by: lbarnett419Posted on 2008-09-28 at 14:24:30ID: 22592627

Ok! Will try!

 

by: lbarnett419Posted on 2008-09-29 at 13:03:52ID: 22600091

Rob:
I started narrowing down the variables using SCHOOLNUM and TYPE and was ok for most. When I would add back SchoolNameDistrict, it would create csv files for some and not others. So I went in the table and updated some of the SchoolNameDistrict names...maybe there were some transparent characters somewhere and it was playing havoc with the path? I don't know, but now I can use all variables and it works.
Now the script is working well, I will modify the code to the last version you offered. This is great, though. Thanks for working so long & patiently with me!

 

by: lbarnett419Posted on 2008-09-29 at 13:04:55ID: 31499347

Thank you Rob!

 

by: RobSampsonPosted on 2008-09-29 at 16:13:19ID: 22601633

Great!  Thanks for your trouble-shooting steps.  Thanks for the grade.

Regards,

Rob.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...