Hello,
I have several unbound forms in an ADP, each with it's own ADO connection calling a stored procedure.
I've heard that you should put your connection information (Standard Module?) in one place and call it to use for different forms but I'm not exactly sure how it's done. Could someone assist me?
Below is an example used on one of my forms from a command button...
So how do I put this in a module, then call the connection instead of having the connection in a bunch of different places?
The stored procedures are sometimes different depending on the form, as are the parameters.
Thanks
Public Sub InsertClass
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim parm As New ADODB.Parameter
On Error GoTo Error_Handler
'Create a new ADO Connection object
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.Access.OLEDB.10
.0"
.Properties("Data Provider").Value = "SQLOLEDB"
.Properties("Data Source").Value = "BH01WF54"
.Properties("Initial Catalog").Value = "HS_PROG_DEV"
.Properties("Integrated Security").Value = "SSPI"
.Open
End With
' Execute command to run stored procedure
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cn
.CommandType = adCmdStoredProc
.CommandText = "usp_InsertClass"
.CommandTimeout = 15
.Parameters.Refresh
.Parameters("@instID") = Me!instID
.Parameters("@classDate") = Me!ClassDate
.Parameters("@classType") = Me!ClassType
.Parameters("@courseType")
= Me!cboCourseType
.Parameters("@classCounty"
) = Me!cboClassCounty
.Parameters("@ttlHrsTaught
") = Me!TtlHrsTaught
.Parameters("@Enrolled") = Me!Enrolled
.Parameters("@Graduated") = Me!Graduated
.Parameters("@schoolHrs") = Me!schoolHrs
.Parameters("@Males") = Me!Males
.Parameters("@White") = Me!White
.Parameters("@Black") = Me!Black
.Parameters("@nativeAm") = Me!NativeAm
.Parameters("@Hispanic") = Me!Hispanic
.Parameters("@Other") = Me!Other
.Parameters("@classComment
s") = Me!Comments
Set rs = .Execute
End With
Do While rs.State = adStateClosed
Set rs = rst.NextRecord
Loop
Me.ClassID = rs!ClassID
If Me.ClassID = rs!ClassID Then
MsgBox "Class record has been created. If present, enter Type 1 Assistants.", , "CONFIRMATION..."
End If
Set cmd = Nothing
Set cn = Nothing
Me.cmdAddAssistant.Visible
= True
Exit Sub
Error_Handler:
MsgBox Err.Description, vbExclamation, "VALID DATA ERROR.."
Exit Sub
End sub
Start Free Trial