ongke0711 > 16-09-20, 10:04 AM
(15-09-20, 06:25 PM)AnNguyen Đã viết: Anh ơi cho em hỏi, trong hàm này là mình sẽ truyền theo từng thamg số tương đương với trong SP đã khai báo rồi, không cần phải khai báo tham số truyền vào là dạng gì nữa phải không ạ?
Và SP em viết trong SQL có nhiều tham số em để mặc định là null ý ạ thì trong cái hàm anh viết kia nếu giá trị nào null phải bỏ qua thì mình khai báo là gì ạ?
Ví dụ em có SP "list_nhanVien" có tham số maHS, nếu em truyền mã học sinh vào thì nó sẽ trả về danh sách của 1 học sinh, nếu em để là null (bỏ trống) thì nó trả về tất cả danh sách chưa học sinh ạ. Thì mình làm như nào ạ?
Set rst = ExecuteSPWithADOCommand("dbo.lst_dmTaiSan", Null, Null) 
Case "Null"
        result.Type = adVarChar
        result.Size = 1
        result.value = Null 
AnNguyen > 16-09-20, 10:37 AM
Set rst = ExecuteSPWithADOCommand("lst_ThongTinNV", Me.txtManv)
    Set lstBox1.Recordset = rstongke0711 > 16-09-20, 11:16 AM
(16-09-20, 10:37 AM)AnNguyen Đã viết: Vâng em cảm ơn anh, chỗ đó ok rồi ạ.
Còn 1 cái bị lỗi nữa anh xem giúp em với ạ
Chỗ này bị lỗi nếu em gán trực tiếp cho 1 subform hoặc 1 listbox thông qua .recordset anh ạ.Mã:Set rst = ExecuteSPWithADOCommand("lst_ThongTinNV", Me.txtManv)
Set lstBox1.Recordset = rst
AnNguyen > 16-09-20, 12:49 PM
(16-09-20, 11:16 AM)ongke0711 Đã viết: Để anh xem lại.Vâng anh xem lại cho em với ạ
Anh thấy em nên khai báo recordset riêng cho mấy cái subform, listbox... vì em dùng chung cái object rst khai báo toàn cục rồi khi có những xử lý khác làm thay đổi Rst này thì lại lỗi dữ liệu.
rst.CursorLocation = adUseClientongke0711 > 16-09-20, 01:57 PM
(16-09-20, 12:49 PM)AnNguyen Đã viết: Nếu khai báo riêng cho mấy cái subform, listbox và viết 1 hàm tương tự như "ExecuteSPWithADOCommand" hàm này anh viết phải không ạ?
Nhưng em thử viết 1 hàm như này rồi cho thêm đoạnvào thì nó báo lỗi ạ.Mã:rst.CursorLocation = adUseClient
    Dim rsLstNhanVien As ADODB.Recordset
    Set rsLstNhanVien = ExecuteSPWithADOCommand("lst_ThongTinNV", Me.txtManv)
    Set Me.ListBox1.Recordset = rsLstNhanVien
    Me.ListBox1.Requery 
Set Me.ListBox1.Recordset = ExecuteSPWithADOCommand("lst_ThongTinNV", Me.txtManv)
Me.ListBox1.Requery 
AnNguyen > 18-09-20, 11:07 AM
ongke0711 > 18-09-20, 11:34 AM
AnNguyen > 18-09-20, 12:38 PM
(18-09-20, 11:34 AM)ongke0711 Đã viết: Hàm đó lúc trước có nhưng đã bỏ Output.
Cụ thể em chạy cái SP nào cần output
    Dim cnn As New ADODB.Connection
    cnn.ConnectionString = Chuoi_Conn
    cnn.Open
    Dim cmd As New ADODB.Command
    Set cmd.ActiveConnection = cnn
    With cmd
        .CommandText = "dbo.Up_MatKhau"
        .CommandType = adCmdStoredProc
        .Parameters.Append .CreateParameter("manv", adVarChar, adParamInput, 12, Manv_Log)
        .Parameters.Append .CreateParameter("matKhau", adLongVarWChar, adParamInput, -1, matKhau)
        .Parameters.Append .CreateParameter("matKhauMoi", adLongVarWChar, adParamInput, -1, MatKhauMoi)
        .Parameters.Append .CreateParameter("@kq", adInteger, adParamOutput, 2)
        .Execute
    End With
    Up_MatKhau = cmd.Parameters("@kq")
    cnn.Close
    Set cnn = Nothingongke0711 > 19-09-20, 10:05 AM
(18-09-20, 12:38 PM)AnNguyen Đã viết:Đây anh ạ, em muốn trả về kêt quả output ạ.Mã:...
Up_MatKhau = cmd.Parameters("@kq")
cnn.Close
Set cnn = Nothing
Public Function ExecuteSPWithADOCommand(StoredProcName As String, OutputParameter As String, OutPutValue As Variant, _
                                        ParamArray InputParameters()) As ADODB.Recordset
    Dim objCmd As ADODB.Command
    Dim rsCmd As ADODB.Recordset
    Dim intParam As Integer
    Dim value As Variant
    'Dim recordsAffected As Long
    'On Error GoTo ESPError
    If ConnectDB() Then
        Set objCmd = New ADODB.Command
        With objCmd
            .ActiveConnection = oConn
            .CommandText = StoredProcName
            .CommandType = adCmdStoredProc
            'Lay tham so
            For intParam = LBound(InputParameters) To UBound(InputParameters)
                value = InputParameters(intParam)
                .Parameters.Append getType(value)
            Next
            
            'Khai bao Output neu co
            If Len(Trim(OutputParameter)) > 0 Then
                .Parameters.Append getTypeOut(OutputParameter, OutPutValue)
            End If
            
            Set ExecuteSPWithADOCommand = .Execute
            
            If Len(Trim(OutputParameter)) > 0 Then
                OutPutValue = .Parameters(OutputParameter).value
            Else
                OutPutValue = vbNullString
            End If
        End With
    Else
        'Khong ket noi
        OutPutValue = vbNullString
    End If
    Exit Function
ESPResume:
    On Error Resume Next
    Set ExecuteSPWithADOCommand = Nothing
    Set objCmd = Nothing
    CloseConnectDB
    Exit Function
ESPError:
    ShowErrorMessages Err, "modKetNoiSQLSv", "ExecuteSPWithADOCommand"
    Resume ESPResume
End Function 
Private Function getTypeOut(ByVal OutputParaName As String, OutPutValue As Variant) As ADODB.Parameter
    Dim result As New ADODB.Parameter
    result.direction = adParamOutput
    result.Name = CStr(OutputParaName)
  
    Select Case TypeName(OutPutValue)
    Case "Integer"
        result.Type = adInteger
    Case "Double"
        result.Type = adDouble
    Case "Date"
        result.Type = adDBDate
    Case "String"
        result.Type = adVarWChar
        result.Size = 255
    End Select
    Set getTypeOut = result
End Function 
Function Up_MatKhau() As Integer
    Dim kq As Integer
    Call ExecuteSPWithADOCommand("Up_MatKhau", "@kq", kq, "annnn", "123456", "123")
    Up_MatKhau = kq
End Function