Downloading data files via a macro.. (1 Viewer)

C

CesarAustin

Guest
I know this can be done but, I just don’t know were to start. What I want to do is create a macro that will automatically down load several comma delimited files from a intra website at work.. What I do now is manually save the files to my HD then the linked file is updated.. is there a way to create a macro to go to that website and upload the files to either my HD or automatically upload to a specified table? :confused: ? Pls help.
 

JimH

Registered User.
Local time
Yesterday, 19:59
Joined
Aug 15, 2004
Messages
19
Take a look at the TransferText Method it might be what your looking for. Also depending on the version of access and options you loaded during install you might have to look at Microsoft Knowledgebase article Q171955.

Here is a whole bunch of code that might/might not help you out. it is pretty well documented, so you can at least see some possibilities. I am not a great programer, so thier is probably better ways to do it. This was behind a button on a form and pulled in a computer doc. HTH Jim


Private Sub btnSansImport_Click()
On Error GoTo btnSansImport_Err


'------------------------------------------------------------
' The following issues need to be resolved in this code:
' 1. The LPOC data thru the SANS people. If they place LPOC data
' into SANS we need to import the data, create a form to display
' during the import to obtain Lat/Long.
' 2. Before production in the 1st module change default value back
' instead of default test name.
' 3. Need to set up the code to splice out the name of the file importing
' to the dateTime of the download and use that time to populate the
' table of data.
'------------------------------------------------------------


'------------------------------------------------------------
' This Code is used to import a SANS .CSV file from the same
' Folder that the database is in. Simply enter SANS and export
' an Excel file to the folder.
'------------------------------------------------------------
On Error Resume Next
DoCmd.DeleteObject acTable, "tblImportedSANSData"
On Error GoTo btnSansImport_Err
Dim tmpErrorImportFileNameToDelete As String

GetFileName = TestIt()
If GetFileName = "" Or IsNull([GetFileName]) Then ' User chose cancel
GoTo btnSansImport_Exit
End If


On Error GoTo btnSansImport_Err

'------------------------------------------------------------
' This Code is used to delete any previous SANS imported data
' from the tblsightings prior to importing any data.
'------------------------------------------------------------
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryDeleteOldSANSRecordsDuringNewRecordImport"
DoCmd.SetWarnings True
statusbartext.Height = "2200"
Me.Requery
statusbartext = "IMPORT STATUS:" & vbCrLf & _
"Successfully deleted old SANS import data..."
Me.Refresh

'------------------------------------------------------------
' This Code is used to import the new SANS data into a
' temporary table for further modification.
'------------------------------------------------------------

DoCmd.TransferText acImportDelim, "", "tblImportedSANSData", _
FindPath() & "\" & [GetFileName] & ".csv", True, ""
tmpErrorImportFileNameToDelete = [GetFileName]
Me.Requery
statusbartext = ("Successfully completed import of SANS table...")
Me.Refresh

'------------------------------------------------------------
' This Code resizes the window to accomidate the user messages.
' The user messages have no real meaning other than to assist
' in determining errors.
'------------------------------------------------------------

DoCmd.MoveSize , , , 4575
Me.FormFooter.Height = "2500"
Me.statusbartext.Visible = True

'------------------------------------------------------------
' This Code is used produce a report of error's encountered during the import.
' Specificly, the errors are those records that SANS did not have a
' Arrival_Port or a COTP_Zone listed.
'------------------------------------------------------------

Msg = "Ship movements logged into the SANS system not having an arrival" _
& "port listed will not be imported into this database. Please " _
& "review, and/or print this list before continuing."
Style = vbYesNo + vbExclamation + vbDefaultButton2 ' Define buttons.
Title = "SOME SHIPS WON'T BE IMPORTED" ' Define title.
' context.
' Display message.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
DoCmd.OpenReport "rptPrintRecordsBeingDeleted", acViewPreview, , , acDialog
'DoCmd.PrintOut acPrintAll
'DoCmd.Close acReport, "rptPrintRecordsBeingDeleted", acSaveYes
Else ' User chose No.
End If
statusbartext = (statusbartext & vbCrLf & "Successfully completed print routine")
Forms!frmsightinglog.Refresh

'------------------------------------------------------------
' This Code Deletes those records that were identified in the report.
' The records are deleted to prevent errors in the the creation.
'
'------------------------------------------------------------

DoCmd.SetWarnings False
DoCmd.OpenQuery "qrydeleteUnknownEntries"
DoCmd.SetWarnings True
statusbartext = statusbartext & vbCrLf & _
"Successfully deleted records with unknown ports..."
Me.Refresh

'------------------------------------------------------------
' This Code modifies the imported table to match the fields contained
' in the tblSightinhLog which it will be appened to later during this
' sub routine.
'------------------------------------------------------------

Dim tbldefs As TableDefs
Dim tbldef As TableDef
Set tbldefs = CurrentDb.TableDefs
Set tbldef = tbldefs("tblImportedSANSData")
With tbldef
.Fields.Append .CreateField("SightLat", dbText, 10)
.Fields.Append .CreateField("SightLong", dbText, 11)
.Fields.Append .CreateField("dataSource", dbText, 1)

End With
statusbartext = statusbartext & vbCrLf & "Successfully Created Fields..."
Me.Refresh

'------------------------------------------------------------
' This Code changes the data type of the Ship_Number field
' from the default number field- to a text field.
' This calls upon a funtion to do the actual work.
'------------------------------------------------------------

Dim dbs As Database, tdf As TableDef, rst As Recordset
Dim fld1 As Field, fld2 As Field, fld3 As Field, prp As Property
Dim i As Integer
' Return reference to current database.
Set dbs = CurrentDb
' Return reference to target table.
Set tdf = dbs.TableDefs("tblImportedSANSData")
' Reference the field you want to rename.
Set fld2 = tdf![Ship Number]
' Change the field name to what I want
fld2.name = "Ship_Number_2"
'We can not change Field Type or Size, they are read-only.
'fld1.Type = dbText
'fld1.Size = 50
'So we have to create a new field with Type and Size we want.
'Here go!
Set fld3 = tdf.CreateField("Ship Number", dbText, 20)
' Append new Field object.
tdf.Fields.Append fld3
' Refresh Fields collection.
tdf.Fields.Refresh
' Delete new Field object.
tdf.Fields.Refresh
'And it's time to add Caption to the newly created field
Set prp = fld3.CreateProperty("Caption", dbText, "Ship Number")
With fld3
.Properties.Append prp
.Properties.Refresh
End With
'Move data from fld2 to fld3
Set rst = dbs.OpenRecordset("tblImportedSANSData")
rst.MoveFirst
For i = 1 To rst.RecordCount
rst.Edit
'rst(2) = rst(1)
rst![Ship Number] = rst!Ship_Number_2
rst.Update
rst.MoveNext
Next i
Set rst = Nothing
'Delete fld 2
tdf.Fields.Delete fld2.name
Set dbs = Nothing
statusbartext = statusbartext & vbCrLf & _
"Successfully converted ship_Nr to TxtField..."
Me.Refresh


***********************SEE NEXT POST*********************
***********************FULL CODE TOO LONG ****************
 

JimH

Registered User.
Local time
Yesterday, 19:59
Joined
Aug 15, 2004
Messages
19
*******************PART TWO ******************************

'------------------------------------------------------------
' This Code goes through each of the records and Fills in the
' blanks of any missing information with identifiable information
' indicating the record had missing information when it was imported.
'------------------------------------------------------------

Dim rst1 As Recordset
Dim db As Database
Set db = DBEngine(0)(0)
Set rst1 = db.OpenRecordset("tblImportedSansData")
If DCount("*", "tblImportedSansData") < 1 Then
MsgBox "There are no records in the table"
DoCmd.Close acTable, "tblImportedSansData"
GoTo btnSansImport_Exit
End If
tmpArrivalPort = "*Destination Unknown*"
tmpshipName = "*Name Unknown*"
tmpShipID = "*ID Unknown*"
' Create new form with Orders table as its record source.
Do While Not rst1.EOF
rst1.Edit
If rst1![Ship Name] = "" Or IsNull(rst1![Ship Name]) Then
rst1![Ship Name] = tmpshipName
End If
If rst1![Ship Number] = "" Or IsNull(rst1![Ship Number]) Then
rst1![Ship Number] = tmpShipID
End If
If rst1![Date Provided] = "" Or IsNull(rst1![Date Provided]) Then
rst1![Date Provided] = rst1![Last Modified]
End If
If rst1![Arrival Port] = "" Or IsNull(rst1![Arrival Port]) Then
rst1![Arrival Port] = tmpArrivalPort
End If
If rst1![Discrepancy] <> "" Then
rst1![Discrepancy] = "Yes"
Else
rst1![Discrepancy] = "No"
End If
If rst1![missing data] <> "" Then
rst1![missing data] = "Yes"
Else
rst1![missing data] = "No"
End If
If rst1![dataSource] = "" Or IsNull(rst1![dataSource]) Then
rst1![dataSource] = 2
End If
If Right("rst1![Arrival Date]", 1) <> "M" Then
tmpctime = "08:00:00 AM"
rst1![Arrival Date] = CDate((CDate(rst1![Arrival Date]) + CDate(tmpctime)))
End If
If Right("rst1![Date Provided]", 1) <> "M" Then
tmpctime = "08:00:00 AM"
rst1![Date Provided] = CDate(rst1![Last Modified])
End If
rst1.Update
rst1.MoveNext
tmpArrivalPort = tmpArrivalPort
Loop
statusbartext = statusbartext & vbCrLf & "Successfully errortrapped missing data..."


Me.Refresh

'------------------------------------------------------------
' This Code appends the newly imported data to the tblSightings table.
'------------------------------------------------------------

On Error Resume Next
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryAppendSANSToSightings"
DoCmd.SetWarnings True
On Error GoTo btnSansImport_Err

statusbartext = statusbartext & vbCrLf & "Successfully appended data to table..."
Me.Refresh
MsgBox "You have sucessfully imported the SANS data"
'------------------------------------------------------------
' This Code Deletes the Errors Import File created during the import
' The records are deleted to prevent errors in the the creation.
'
'------------------------------------------------------------

DoCmd.DeleteObject acTable, ([tmpErrorImportFileNameToDelete] & "_ImportErrors")


'------------------------------------------------------------
' This Code breaksdown the imported SANS file string name
' that was imported and changes it to a date format. Then
' updates the table called above to determine the last time
' an SANS import was done.
'------------------------------------------------------------
ckflLen = Len([GetFileName])
If [ckflLen] = 14 Then
yr = Left([ckflLen], 4)
mth = Mid([ckflLen], 5, 2)
dy = Mid([ckflLen], 7, 2)
hr = Mid([ckflLen], 9, 2)
mn = Mid([ckflLen], 11, 2)
sc = Right([ckflLen], 2)
tm = CDate(mth & "/" & dy & "/" & yr & " " & hr & ":" & mn & ":" & sc)

Else
tm = Now()
End If

Set rst2 = db.OpenRecordset("tbllastImportData")
Do While Not rst2.EOF
rst2.Edit
rst2.lastImportDTG = [tm]
rst2.Update
rst2.MoveNext
Loop
statusbartext = statusbartext & vbCrLf & "Successfully updated last import date/time..."




'------------------------------------------------------------
' This Code resizes the window
'------------------------------------------------------------
Wait (2)
Me.statusbartext.Visible = False
statusbartext.Height = "50"
DoCmd.MoveSize , , , 2320
Me.FormFooter.Height = "220"




' Restore form.
btnSansImport_Exit:
Exit Sub

btnSansImport_Err:
MsgBox Error$
Resume btnSansImport_Exit

End Sub
 

Users who are viewing this thread

Top Bottom