Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

accesss链接

ll  发表于:2018-01-24 09:00:59  
复制

Option Compare Database
Option Explicit




Public Function AttachExcel( _
    ByVal sFileName As String, _
    ByVal sTablename As String, _
    ByVal sRangeName As String _
    ) As Boolean
  
  Const conCannotOpen = 3432
  Const conNotRange = 3011
  Const conTableExists = 3012


  Dim db As DAO.Database
  Dim td As DAO.Tabledef
  
  Dim sConnect As String
  Dim sMsg As String
  Dim sFunction As String
  
On Error GoTo HandleError
  
  AttachExcel = False
  sFunction = "AttachExcel"
  
  ' Check for existence of worksheet:
  sFileName = CurDir() & "\" & sFileName
  
  ' If the file isn't found, notify
  ' the user and exit the procedure:
  If Len(Dir(sFileName)) = 0 Then
    MsgBox "The file " & sFileName _
        & " could not be found"
    MsgBox "Please move the file to " _
        & CurDir() & " to continue"
    Exit Function
  End If
  Set db = CurrentDb
  
  ' Create a new tabledef in the current database:
  Set td = db.CreateTableDef(sTablename)
  
  ' Build connect string:
  sConnect = "Excel 8.0;HDR=YES;DATABASE=" & sFileName
  td.Connect = sConnect
  
  ' Specify Range Name sRangeName:
  td.SourceTableName = sRangeName
  
  ' Append new linked table to TableDefs collection:
  db.TableDefs.Append td
  
  'Return True:
  AttachExcel = True
  
ExitHere:
  
  Exit Function
  
HandleError:
  
  Select Case Err
    Case conCannotOpen
        sMsg = "Cannot open " & sFileName
  
    Case conTableExists
        sMsg = "The table " & sTablename & _
             " already exists."
  
    Case conNotRange
        sMsg = "Can't find the " & sRangeName & " range."
  
    Case Else
        sMsg = "Error#" & Err & ": " & Error$
  
  End Select
  
  MsgBox sMsg, vbExclamation + vbOKOnly, _
       "Error in Procedure " & sFunction
  
  'Return False:
  AttachExcel = False
  
  Resume ExitHere
  

End Function

这是链接Excel文件的函数,红色代码中的语句如何理解?

另外sourcename和定义中的stablename 是不是一回事?

 

Top
MDZZ 发表于:2018-01-24 13:08:52
一个是链接的excel中的表名 一个是链接到access后链接表的名称   可以不一致

总记录:1篇  页次:1/1 9 1 :