好的,所以我试图将一些代码添加到连接到访问数据库以进行数据收集的现有ASP网页。代码应该检查'fleet'表中的不同'modecode'变量是否与'equipment'表匹配。如果缺少“设备”模式,则会添加缺少的模式。我遇到的问题是,在第一次加载页面时,它会抛出错误,但会添加正确的记录。然后,如果您重新加载页面,它将正常显示。代码如下。
编辑:当我添加新记录时,错误是否可能没有定义'Equiment'中的每个变量?
编辑:我得到的错误只是'500 - 内部服务器错误'而且我不确定如何诊断。
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "vehicles","",""
Dim modeArr() 'array to hold modecodes
Dim i 'array index
Dim needToClickUpdate 'If an Equipment # > than a Fleet #,
Dim k 'for loop index
needToClickUpdate = ""
'Get the total # of active vehicles by mode from the fleet table.
strFleet = "SELECT Sum(Number) As NumVehPerMode, modecode FROM fleet WHERE MemID=" & Request("MemID") & " And status='A' GROUP BY modecode ORDER BY modecode"
Set rstFleet = Server.CreateObject("ADODB.Recordset")
rstFleet.Open strFleet, conn, 3, 3
If rstFleet.RecordCount > 0 Then
rstFleet.MoveFirst
Dim TotNumVeh() 'array to hold # vehicles per mode
Dim TotModLis() 'array with list of modes
Dim TotModLis2()
i = 0
while Not rstFleet.EOF
Redim Preserve TotNumVeh(i+1)
TotNumVeh(i) = rstFleet("NumVehPerMode")
Redim Preserve TotModLis(i+1)
TotModLis(i) = rstFleet("modecode")
Redim Preserve TotModLis2(i+1)
TotModLis2(i) = rstFleet("modecode")
i = i + 1
rstFleet.MoveNext
wend
End If
rstFleet.Close
'This section grabs which modes have equipment records
strEquipmodecheck = "SELECT modecode FROM Equipment WHERE MemID=" & Request("MemID") & " AND modecode IN ('" & Join(TotModLis, "','") & "') ORDER BY modecode"
rstFleet.Open strEquipmodecheck, conn, 3, 3
If rstFleet.RecordCount > 0 Then
rstFleet.MoveFirst
Dim n
Dim m
Dim Equipmodes()
Dim MissingModes : Missingmodes = Array()
Dim isFound
m = 0
n = 0
p = 0
while Not rstFleet.EOF
Redim Preserve Equipmodes(m+1)
Equipmodes(m) = rstFleet("modecode")
m = m + 1
rstFleet.MoveNext
wend
End If
rstFleet.Close
'This section compares the mode lists from Fleet and Equipment and writes a list of Missing Modes
For m = 0 To UBound(TotModLis2) - 1
isFound = False
For n = 0 To UBound(Equipmodes) - 1
If TotModLis2(m) = Equipmodes(n) Then
isFound = True
End If
Next 'j
If Not isFound Then
Redim Preserve MissingModes(p+1)
MissingModes(p) = TotModLis2(m)
p = p + 1
End If
Next 'i
'This section adds equipment records based on the list of missing modes
newequips = "SELECT * FROM Equipment"
rstFleet.Open newequips, conn, 3, 3
For Each x in MissingModes
rstFleet.Addnew
rstFleet("MemID") = Request("MemID")
rstFleet("modecode") = x
rstFleet.Update
Next
rstFleet.Close