You can send-say- one record from your Access database (with SQL or Access back-end data) to Excel. You can then email the Excel file to a supplier for them to fill in the fields. When they return the file you can re-import it. These code snippets show how you might do this.
You can get a small database containing all the code below from flikkImportExport.zip
Exporting a record
Prompt the user for a template to use. I used my own nOpenFile function:
i = nOpenFile(Me.hwnd, filt, templateName, ft, “xltx”, dialogtitle, “”)
Create an Excel.Application object, get the wb (WorkBook) and ws (WorkSheet) you are going to use.
Set excelApp = CreateObject(“Excel.Application”) ‘every time this runs you get a new Excel app.
‘may be better to store excelApp in global variable and only set it if it’s not set
‘you have to trap for errors in case the user has closed it…
excelApp.Visible = True
If Len(templateName) > 0 And Len(Dir(templateName, vbNormal)) > 0 Then
Set wb = excelApp.Workbooks.Add(Template:=templateName)
Else
Set wb = excelApp.Workbooks.Add()
End If
Set ws = wb.Worksheets(1)
Look for range “data_here” which should be defined in the template you used. If you didn’t use a template, start the data in B1.
Set theRange = wb.Worksheets(1).range(“b1”)
On Error Resume Next
Set theRange = wb.Names(“data_here”).RefersToRange
On Error GoTo 0
‘for every field in the recordset / record
For col = 0 To rs.Fields.Count – 1
‘get the fieldname
fieldName = rs.Fields(col).Name ‘name of recordset field
‘put that into cell A1
ws.Cells(r, c – 1).Formula = fieldName ‘label cell
‘put the value of the recordset field into cell B1
ws.Cells(r, c).Formula = “=””” & rs.Fields(col).Value & “””” ‘value cell
‘name the range B1 the same as the fieldname
wb.Names.Add Name:=fieldName, RefersToR1C1:=”=” & ws.Name & “!R” & r & “C” & c ‘name the value cell
‘on to the next row
r = r + 1
Next
Now you have a spreadsheet / worksheet with two columns: A is the field names and B is the field values. Each cell in B is also a named range, the name being the field name.
My spreadsheet looks like this…
Send this to the suppliers and ask them to fill in the fields. If you dont’ want them to see fields, just delete those rows, together with the named ranges. You can of course alter the code so these fields don’t see the light of day.
Some time later, the spreadsheet comes back.
Press the Import button. This code runs…
Set rs = Me.Recordset
filt = “Excel Files|*.xl*|All Files|*.*”
dialogtitle = “Select incoming spreadsheet”
i = nOpenFile(Me.hwnd, filt, fileName, ft, “xlsx”, dialogtitle, “”)
If Len(fileName) > 0 And Len(Dir(fileName, vbNormal)) > 0 Then
Set excelApp = CreateObject(“Excel.Application”) ‘every time this runs you get a new Excel app.
‘may be better to store excelApp in global variable and only set it if it’s not set
‘you have to trap for errors in case the user has closed it…
excelApp.Visible = True
Set wb = excelApp.Workbooks.Open(fileName)
Now wb is the workbook to import. It has a set of Names and these correspond with the fieldnames in your table.
One field contains your primary key. You can find the value of that as follows…
‘todo: make this suit your own data
‘find the named PRODUCTID, then choose that record to edit
fieldName = “ProductId”
Dim ProductID As Long
ProductID = wb.Names(fieldName).RefersToRange.Value2
‘productId should be the ID you want to edit
If you’ve got the primary key, you can find the relevant record…
If (ProductID > 0) Then
rs.FindFirst (“ProductID=” & ProductID)
If rs.NoMatch Then
Else
Now you can edit the fields one-by-one
rs.Edit ‘TODO: this is only necessary for a DAO Recordset
For i = 1 To wb.Names.Count
fieldName = wb.Names(i).Name
‘todo: ignore fieldNames you can’t update
Select Case fieldName
Case “data_here”
‘do nothing
Case Else
On Error Resume Next
rs(fieldName).Value = wb.Names(fieldName).RefersToRange.Value2
On Error GoTo 0
End Select
Next