将excel编号转换为访问VBA中的短文本

时间:2018-01-29 15:44:23

标签: excel vba excel-vba ms-access-2016

您好我有一个访问表,可以从excel获取数据

Public Sub ImportExcel(fileName As String)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "JobNumber", fileName, False, "K2:K2"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "DateTable", fileName, False, "C3:C3"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Supplier", fileName, False, "C5:C5"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Graphic", fileName, False, "C6:C6"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Shift", fileName, False, "L1:L1"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Size", fileName, False, "L3:L3"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "RollInfo", fileName, False, "B10:F102"
Call CheckNullValues
Call AddLookUps
Call AddPressData
Call deleteZigZagTables
End Sub

我在将Excel中的C6值转换为访问文本时遇到问题我现在正在做的是这个

Public Sub AddPressData()
Dim SQLstring As String
Dim GraphicString As String
Dim rst As Recordset

SQLstring = "SELECT F1 FROM GRAPHIC;"
Set rst = CurrentDb.OpenRecordset(SQLstring)
If Not IsNumeric(rst.Fields(0).Value) Then
    SQLstring = "INSERT INTO Press ( MasterRollNumber, JobNumber, DateDay, Shift, Supplier, Graphic, [Size], [Linear Measure], [Pallet Number] ) SELECT RollInfo.F1, JobNumber.F1, DateTable.F1, Shift.F1, Supplier.F1, Graphic.F1, (Size.F1), RollInfo.F3, RollInfo.F4 FROM RollInfo, JobNumber, DateTable, Shift, Supplier, Graphic, [Size] WHERE NOT ISNULL(RollInfo.F1);"
    CurrentDb.Execute SQLstring, dbFailOnError
Else
    SQLstring = "INSERT INTO Press ( MasterRollNumber, JobNumber, DateDay, Shift, Supplier, Graphic, [Size], [Linear Measure], [Pallet Number] ) SELECT RollInfo.F1, JobNumber.F1, DateTable.F1, Shift.F1, Supplier.F1, CSTR(Graphic.F1), (Size.F1), RollInfo.F3, RollInfo.F4 FROM RollInfo, JobNumber, DateTable, Shift, Supplier, Graphic, [Size] WHERE NOT ISNULL(RollInfo.F1);"
    CurrentDb.Execute SQLstring, dbFailOnError
End If
End Sub

大多数情况下,graphic是一个字符串,但每隔一段时间它就可以成为一个数字,当我运行代码时,它只是一个数字,只会让每个记录的字段为空。

如果有人有任何建议会有很大帮助 谢谢

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点,但这是我的方式,因为那时你根本不需要 Graphic.F1 & '' 语句。

CSTR

将字符串与数字连接时,结果为字符串。

from threading import * mutex = RLock() # RLock so only one thread can lock and unlock global read_count, writer_count # Keep track of number of reading and writting threads read_lock = Lock() # Updated write_lock = Lock() # Updated # Initialize the locks read_count = 0 # Updated writer_count = 0 # Updated def read_from_db(self, sql): mutex.acquire() global read_count, writer_count if writer_count > 0: mutex.release() #release mutex to avoid deadlock read_lock.acquire() # Get the read_lock or wait until able to get it else: mutex.release() mutex.acquire() if read_count == 0: write_lock.acquire() # Get the write lock so writers can't get it while read thread is reading read_count += 1 mutex.release() # read from DB mutex.acquire() read_count -= 1 if read_count <= 0: write_lock.release() #release the write lock mutex.release() def write_to_db(self): global read_count, writer_count mutex.acquire() if read_count > 0: mutex.release() #release mutex to avoid deadlock write_lock.acquire() #get the write lock or wait until able to get it else: mutex.release() mutex.acquire() if writer_count <= 0: read_lock.acquire() #Get the read_lock so that no reads happen while writing writer_count += 1 mutex.release() # write to db mutex.acquire() writer_count -= 1 if writer_count == 0: # Updated read_lock.release() mutex.release() 是一个VBA函数,您正在尝试在SQL语句中使用它。

答案 1 :(得分:0)

在构造SQL字符串时,您可以使用连接运算符和字符串转换函数,例如:

s = "Hello " & CStr(Cells(1, 1)) & "world"

注意:我强烈建议您格式化SQL字符串,以便更好地阅读。例如:

    SQLstring = _
        "INSERT INTO Press( MasterRollNumber, JobNumber, DateDay, Shift, Supplier, Graphic, [Size], [Linear Measure], [Pallet Number] ) " & _
        "      SELECT RollInfo.F1, JobNumber.F1, DateTable.F1, Shift.F1, Supplier.F1, Graphic.F1, (Size.F1), RollInfo.F3, RollInfo.F4 " & _
        "      FROM RollInfo, JobNumber, DateTable, Shift, Supplier, Graphic, [Size] " & _
        "      WHERE NOT ISNULL(RollInfo.F1);"