Hi Guys,
Was wondering if I could get some insight into why my script isn't working. I have a job related excel form where ive placed some script so that at the press of a button all the important information is automatically loaded onto another excel spreadsheet. The coding works for the first 2 rows of the spread sheet but after that I receive a "runtime error '-2147467259 (80004005)': Cannot expand named range." error message. Heres the code used;
Public cnn As New ADODB.Connection
Sub ConnectDB()
If cnn.State <> adStateOpen Then
Dim strFileName As String
strFileName = [ConfigPath].Value
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFileName & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
End If
End Sub
Sub CopyData()
ConnectDB
Dim rst As New ADODB.Recordset
Dim CurrentID As Long
Dim vbResult As VbMsgBoxResult
rst.Open "SELECT * FROM [Database];", cnn, adOpenStatic, adLockReadOnly
If Not rst.EOF Then
rst.MoveLast
CurrentID = rst("RefNumber")
CurrentID = CurrentID + 1
Else
CurrentID = 1
End If
Dim sql As String
sql = "Insert into [Database] (refnumber, surname, FirstName, DOB, EthnicOrigin, School, Address, Postcode, ReferrerEmail, PreferredSetting) values('" & _
CurrentID & "','" & [FormSurname] & "','" & [FormFirstName] & "','" & [FormDOB] & "','" & [FormEthnic] & "','" & [FormSchool] & "','" & _
[FormAddress] & "','" & [FormPostCode] & "','" & [FormEmail] & "','" & [FormPreferred] & "')"
cnn.Execute (sql)
MsgBox "Record Saved to Database"
DisconnectDB
End Sub
Sub DisconnectDB()
If cnn.State <> adStateClosed Then
cnn.Close
Set cnn = Nothing
End If
End Sub
Its slowly driving me insane and I cannot work out why it wont work!!. Ive attached pics any help would be hugely appreciated!! The form has already been distributed to some 500-600 people from when it was working properly. So i'm hoping (and praying) its a fault with the spreadsheet the data's been sent too and not the form itself. I have noticed that the named "database" range wont expand as new data is being inserted into it....not sure if its relevant
Many Thanks
Sam
Attachment 97433Attachment 97435Attachment 97437
Was wondering if I could get some insight into why my script isn't working. I have a job related excel form where ive placed some script so that at the press of a button all the important information is automatically loaded onto another excel spreadsheet. The coding works for the first 2 rows of the spread sheet but after that I receive a "runtime error '-2147467259 (80004005)': Cannot expand named range." error message. Heres the code used;
Public cnn As New ADODB.Connection
Sub ConnectDB()
If cnn.State <> adStateOpen Then
Dim strFileName As String
strFileName = [ConfigPath].Value
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFileName & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
End If
End Sub
Sub CopyData()
ConnectDB
Dim rst As New ADODB.Recordset
Dim CurrentID As Long
Dim vbResult As VbMsgBoxResult
rst.Open "SELECT * FROM [Database];", cnn, adOpenStatic, adLockReadOnly
If Not rst.EOF Then
rst.MoveLast
CurrentID = rst("RefNumber")
CurrentID = CurrentID + 1
Else
CurrentID = 1
End If
Dim sql As String
sql = "Insert into [Database] (refnumber, surname, FirstName, DOB, EthnicOrigin, School, Address, Postcode, ReferrerEmail, PreferredSetting) values('" & _
CurrentID & "','" & [FormSurname] & "','" & [FormFirstName] & "','" & [FormDOB] & "','" & [FormEthnic] & "','" & [FormSchool] & "','" & _
[FormAddress] & "','" & [FormPostCode] & "','" & [FormEmail] & "','" & [FormPreferred] & "')"
cnn.Execute (sql)
MsgBox "Record Saved to Database"
DisconnectDB
End Sub
Sub DisconnectDB()
If cnn.State <> adStateClosed Then
cnn.Close
Set cnn = Nothing
End If
End Sub
Its slowly driving me insane and I cannot work out why it wont work!!. Ive attached pics any help would be hugely appreciated!! The form has already been distributed to some 500-600 people from when it was working properly. So i'm hoping (and praying) its a fault with the spreadsheet the data's been sent too and not the form itself. I have noticed that the named "database" range wont expand as new data is being inserted into it....not sure if its relevant
Many Thanks
Sam
Attachment 97433Attachment 97435Attachment 97437