Upload large file to Microsoft Access - ms-access-2010

I am quite new to setup a MS Access data base. Just wondering whether there is a way to upload a coma delimited file with more than 1.5 million rows and ignore the first 3 lines (file header) and the last row (footer).
The header for the content of this file is at the 4th row.

Finally i worked it out myself.
the header and the footer are having different number of columns.
I used line input statement to check each line of the my text file.
Here is my code:
Sub FileUpload_CMP_Funding()
Dim sFile, sText As String
Dim dText As Variant
Dim db As Database
Dim rst As Recordset2
Dim i As Long
sFile = "C:\NotBackedUp\testfile\CMPFUNding.out"
Open sFile For Input As #1
Do While Not EOF(1)
Line Input #1, sText
dText = Empty
dText = Split(vText(i), ",")
'My main content has 24 columns
If UBound(dText) - LBound(dText) + 1 = 24 Then
If dText(0) <> "Product ID" Then 'skip the header row at the 4th rows
Set db = CurrentDb
Set rst = db.OpenRecordset("tblCMP_Funding", dbOpenDynaset)
rst.AddNew
rst!ProductID = Trim(Replace(dText(0), """", ""))
rst!FundID = Trim(Replace(dText(1), """", ""))
""
'Update whatever field is required to be updated
rst.Update
Set db = Nothing
Set rst = Nothing
End If
End If
Loop
Close #1
End Sub
Hope it helps anyone who have same requirement

Related

Using R to Automate Filename Retrieval in a Microsoft Word Table

I have a large table within a Microsoft Word document.
The majority of rows, but not all, have a single Microsoft Word file attached.
My job is to go into each row and manually type in the file name where an attachment is provided.
Is there any way to automate this task using an R package? For example, for each row that has a file attachment, automatically pull the filename and record it in the field directly to its left?
This is what the table looks like. The files are in the most right column. The column to its left is where I am going to be typing the filenames.
I've tried importing the docx file using the docxtractr package, but it is not reading in the filenames properly. Instead, it is replacing them with \s.
ievs_raw <- read_docx("ievs-raw.docx")
tbls <- docx_extract_all_tbls(real_world)
view(as.data.frame.list(tbls))
Produces the following output with \s where there should be filenames like CAP_ATT_H.11.114.docx etc.:
I wasn't able to figure this out using an R package, but the kind people at the Microsoft Community Forum helped out by providing a super useful Visual Basic Macro. What's great about this is it can accommodate cases where there is more than 1 attachment in a particular row.
Sub ObjectNames()
Dim ILS As InlineShape
Dim nObj As Long
Dim strName As String
Dim col As Long
Dim row As Long
With ActiveDocument.Tables(1)
col = .Columns.Count
For row = 1 To .Rows.Count
strName = ""
# loop through all shapes in this row's last cell
# (if there are none, the loop does nothing)
For nObj = 1 To .Cell(row, col).Range.InlineShapes.Count
Set ILS = .Cell(row, col).Range.InlineShapes(nObj)
If Not ILS.OLEFormat Is Nothing Then
# build up a string with as many names as
# there are embedded objects, separated by
# paragraph marks (vbCr)
If nObj > 1 Then strName = strName & vbCr
strName = strName & ILS.OLEFormat.IconLabel
End If
Next nObj
If Len(strName) > 0 Then
.Cell(row, col - 1).Range.Text = strName
End If
Next row
End With
End Sub

Website won't release file generated with openxml

Here is the situation:
Asp.Net Web Forms site using Open XML to read in a (via a stream) word document (docx). I then insert some text into the document and then write the file back out to a different location. It is then emailed to an end user. All of this works great.
The problem i am running into is that I can't the new file written by the site. I receive the following error:
"The process cannot access the file (file name here) because it is being used nt another process"
I have confirmed that it is the site (or IIS) that is holding on to the file.
Here is the code that reads the original file and generates the new file:
Private Function GetDocument(worddoc As String) As Integer
Dim byteArray As Byte() = File.ReadAllBytes("\\WEB-DEV-1\HR_Documents\" & worddoc)
Using Stream As New MemoryStream()
Stream.Write(byteArray, 0, CInt(byteArray.Length))
Try
'Set Row & Cell variables
Dim rowNum As Integer = 0
Dim cellNum As Integer = 0
'Set File Stream
Using doc As WordprocessingDocument = WordprocessingDocument.Open(Stream, True)
'Employee Name Insert
'Find first table in document
Dim tbl1 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
'First Row in tbl
Dim row As TableRow = tbl1.Elements(Of TableRow)().ElementAt(0)
'Find first cell in row
Dim cell As TableCell = row.Elements(Of TableCell)().ElementAt(0)
'Insert selected Employee Name
Dim p As Paragraph = cell.Elements(Of Paragraph)().First()
Dim r As Run = p.Elements(Of Run)().First()
Dim txt As Text = r.Elements(Of Text)().First()
txt.Text = "Employee Name: " & ddlEmployeeList.SelectedItem.Text
'Supervisor Name Insert
'Check for form
If ddlFormChoice.SelectedIndex <> 2 Then
'Reset row to supervisors row in table
row = tbl1.Elements(Of TableRow)().ElementAt(1)
ElseIf ddlFormChoice.SelectedIndex = 2 Then
'Reset row to supervisors row in table
row = tbl1.Elements(Of TableRow)().ElementAt(2)
End If
If ddlFormChoice.SelectedIndex <> 2 Then
'Reset cell to supervisor cell in row
cell = row.Elements(Of TableCell)().ElementAt(1)
ElseIf ddlFormChoice.SelectedIndex = 2 Then
'Reset cell to supervisor cell in row
cell = row.Elements(Of TableCell)().ElementAt(0)
End If
'Insert selected Employee Name
p = cell.Elements(Of Paragraph)().First()
r = p.Elements(Of Run)().First()
txt = r.Elements(Of Text)().First()
If ddlFormChoice.SelectedIndex <> 2 Then
txt.Text = "Supervisor: " & ddlSupervisorList.SelectedItem.Text
ElseIf ddlFormChoice.SelectedIndex = 2 Then
txt.Text = "Manager/Supervisor: " & ddlSupervisorList.SelectedItem.Text
End If
doc.Close()
End Using
'Save File to temp location
File.WriteAllBytes("\\WEB-DEV-1\HR_Documents\TempDocs\" & worddoc, Stream.ToArray())
Stream.Close()
Stream.Dispose()
Return 1
Catch ex As Exception
Return Nothing
End Try
End Using
End Function
I close the OpenXML doc and the stream as well dispose of the stream but when I try to delete the file from the main sub that called the function I get the error listed above.
What am I missing?? I closed the doc, the stream and disposed of the stream. Why is the site still holding the file?
Note here the line of code that trys to delete the file;
File.Delete("\\Web-Dev-1\HR_Documents\TempDocs\" & fileAttach)
So after most of the day i finally found out what the problem was. After the document was created, saved , and emailed it was being held by the email method. For some reason i thought that when the method finishes that it disposed of the Mail Message but this not the case.
Once I added the dispose line it all worked fine.
Only been Googling for almost two days. :|

Open XML SDK Open and Save not working

I am having trouble with the Open XML SDK opening and saving word documents.
I am using the following code (VB.Net):
Try
'Set Path
Dim openPath As String = "../Documents/" & worddoc
Dim savePath As String = "\\web-dev-1\HR_Documents\" & worddoc
Using doc As WordprocessingDocument = WordprocessingDocument.Open("/Documents/" & worddoc, True)
'Employee Name Insert
'Find first table in document
Dim tbl1 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
'First Row in tbl
Dim row As TableRow = tbl1.Elements(Of TableRow)().ElementAt(0)
'Find first cell in row
Dim cell As TableCell = row.Elements(Of TableCell)().ElementAt(0)
'Insert selected Employee Name
Dim p As Paragraph = cell.Elements(Of Paragraph)().First()
Dim r As Run = p.Elements(Of Run)().First()
Dim txt As Text = r.Elements(Of Text)().First()
txt.Text = ddlEmployeeList.SelectedItem.Text
'Save File
'Supervisor Name Insert
'Find second table in document
Dim tbl2 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
'First Row in tbl
Dim row2 As TableRow = tbl2.Elements(Of TableRow)().ElementAt(0)
'Find first cell in row
Dim cell2 As TableCell = row2.Elements(Of TableCell)().ElementAt(0)
'Insert selected Employee Name
Dim p2 As Paragraph = cell2.Elements(Of Paragraph)().First()
Dim r2 As Run = p2.Elements(Of Run)().First()
Dim txt2 As Text = r2.Elements(Of Text)().First()
txt2.Text = ddlSupervisorList.SelectedItem.Text
End Using
Return 1
Catch ex As Exception
Return Nothing
End Try
The trouble starts on the first using statement. It throws the following error:
Could not find a part of the path 'C:\Documents\Hourly_Employee_Performance_Review .docx
I have placed the word documents in a folder of the ASP.NET site called Documents. I also have created a public share on the dev server to see if maybe that would help.
The problem is that it doesn't use the supplied path variable. I have gone through the documentation for OPEN XMl SDK but all it talks about is the Using Statement and its need and use for it.
Can anyone tell me, show me, or point to a site that has examples of how to set both the open path and save path?
You need a path to the file which is based on the filesytem, not a URL. You can do that with
Dim openPath As String = Path.Combine(Server.MapPath("~/Documents"), worddoc)
And then to open the file:
Using doc As WordprocessingDocument = WordprocessingDocument.Open(openPath, True)
It appears that you will need to do the same for the location to save to, but you didn't say if "\\web-dev-1" is a different server; if it were that would need other considerations.
(Not tested, some typos may exist. You will need an Imports System.IO.)

OpenXML asp.net loop through header

I have a 10 page document. Each page has a header and footer. The header might have two lines of text or one, with each one having a different style. What a want to do is to loop through the document. Read the header and footer of each page, put that into a DataTable so I can build a TOC later on. Any idea, I tried but it's not working correctly, it's not reading each page footer and skipping pages(It seems out of order and I'd like to get the values in the page order since it seems to skip the first page until the last iteration).
Help would be appreciated. JT
Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(combineDocName, True)
For Each Head As HeaderPart In wordDoc.MainDocumentPart.HeaderParts
For Each currentParagraph As DocumentFormat.OpenXml.Wordprocessing.Paragraph In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
Dim p As ParagraphProperties = currentParagraph.Elements(Of ParagraphProperties)().First()
If p.Count > 0 Then
If (p.ParagraphStyleId IsNot Nothing) Then
If p.ParagraphStyleId.Val.ToString() = "HeaderBar" Then
For Each currentText As Text In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
If (String.IsNullOrEmpty(keepHeaderM)) Then
HeaderBarTxt = currentText.Text.Trim()
ElseIf keepHeaderM <> currentText.Text.Trim() Then
HeaderBarTxt = currentText.Text.Trim()
End If
Next
ElseIf p.ParagraphStyleId.Val.ToString() = "NavigationBar" Then
For Each currentText As Text In Head.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
iCount = currentText.Text.Split(":").Length - 1
If (String.IsNullOrEmpty(keepHeaderM)) Then
HeaderTxt = currentText.Text.Trim()
ElseIf keepHeaderM <> currentText.Text.Trim() Then
HeaderTxt = currentText.Text.Trim()
End If
Next
End If
End If
End If
Next
Next
For Each foot As FooterPart In wordDoc.MainDocumentPart.FooterParts
For Each currentParagraph2 As DocumentFormat.OpenXml.Wordprocessing.Paragraph In foot.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
If currentParagraph2.Count > 0 Then
For Each currentText2 As Text In foot.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
Dim strTemp As String = currentText2.Text
If strTemp.IndexOf("-") <> -1 Then
FooterTxt = currentText2.Text.Trim()
End If
Next
End If
Next
Next
end using
If you have different headers and footers in the Word file, then I'm assuming that there are multiple sections in your Word document.
You'll have to iterate through these sections and then read the footers and headers in the sections and then save them somewhere in a DataTable.
Coming to the question of creating a TOC.
What you could do is create a separate word document with a TOC created manually say TOC.docx.
Now when you want to add the TOC, you create a copy of TOC.docx and then merge this file with document in which you want to TOC.
Don't forget to set the auto update to true in the settings of the merged document.
<w:updateFields w:val="true" />

How can i design a CSV file using VB?

In my project I am creating a CSV file, but I want to change it's design. Please help me:
Private Sub ExportDataToCSV()
Dim fileName As String = "CheckRegistrationStatus_" & Format(Now, "yyyyMMddhhmms") & ".csv"
HttpContext.Current.Response.Clear()
' Set the response headers to fit our CSV file
HttpContext.Current.Response.ContentType = "text/plain"
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)
Using writer As New System.IO.StreamWriter(HttpContext.Current.Response.OutputStream)
Dim columnHeader As String = String.Empty
For i As Integer = 0 To grd1.Columns.Count - 1
columnHeader += grd1.Columns(i).HeaderText & IIf(i < grd1.Columns.Count - 1, ",", "").ToString()
Next
writer.WriteLine(columnHeader)
'writer.WriteLine(AddCSVHeaderRow()) ' Only if you need custom headers to be added
' Add all the data rows
For Each row As GridViewRow In grd1.Rows
writer.WriteLine(GetCSVLine(row.Cells))
Next
End Using
' End the current response. Otherwise, excel will open with the whole page inside.
HttpContext.Current.Response.End()
End Sub
Private Shared Function GetCSVLine(ByVal cellsToAdd As TableCellCollection) As String
Dim line As String = String.Empty
Dim isFirst As Boolean = True
For Each cell As TableCell In cellsToAdd
If Not isFirst Then
line += ","
End If
isFirst = False
line += """" & Replace(cell.Text, " ", "") & """"
Next
Return line
End Function
Output is being displayed as shown in the following image. But I want to make the header bold and expand the column width . Please help me.
You cannot. The CSV file format is a data-only format. It provides no way to set fonts, column widths or anything else related to styling.
In addition, I don't think your code handles all data correctly. For example, if there's a comma within the data or a double quote, special steps are required. Here's some code I published for creating CSV files in C#.
If you want to produce a formatted Excel document, either in addition to your CSV file or in place of it, you could have a look at the Excel interop libraries.
http://msdn.microsoft.com/en-us/library/bb386107%28v=vs.90%29.aspx
http://support.microsoft.com/kb/301982
http://msdn.microsoft.com/en-us/library/aa188489%28office.10%29.aspx

Resources