Click here to Skip to main content
15,892,005 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionVb6 sybntax counterpart of VB.Net. Pin
priyamtheone12-Jun-10 6:33
priyamtheone12-Jun-10 6:33 
AnswerRe: Vb6 sybntax counterpart of VB.Net. Pin
Abhinav S12-Jun-10 7:29
Abhinav S12-Jun-10 7:29 
AnswerRe: Vb6 sybntax counterpart of VB.Net. Pin
Anshul R12-Jun-10 19:10
Anshul R12-Jun-10 19:10 
GeneralRe: Vb6 sybntax counterpart of VB.Net. Pin
Eddy Vluggen12-Jun-10 21:49
professionalEddy Vluggen12-Jun-10 21:49 
AnswerRe: Vb6 sybntax counterpart of VB.Net. Pin
Eddy Vluggen12-Jun-10 21:48
professionalEddy Vluggen12-Jun-10 21:48 
QuestionRe: Vb6 sybntax counterpart of VB.Net. Pin
priyamtheone14-Jun-10 6:25
priyamtheone14-Jun-10 6:25 
AnswerRe: Vb6 sybntax counterpart of VB.Net. Pin
Eddy Vluggen14-Jun-10 8:25
professionalEddy Vluggen14-Jun-10 8:25 
QuestionRe: Vb6 sybntax counterpart of VB.Net. Pin
priyamtheone17-Jun-10 5:37
priyamtheone17-Jun-10 5:37 
Thanks Eddy, implemented your idea. My problem is almost solved except a small bug. I have a data entry form with certain fields among which is a datetimepicker. The idea is while inserting or updating records through that form the application will first check whether the datetimepicker is checked. If yes then it'll pass the value of datetimepicker to the command parameter or else it'll pass null value to the command parameter. The corresponding database table column for the datetimepicker field has smalldatetime data type. First take a look into my code:

Private con As ADODB.Connection

Private Sub cmdOk_Click()
    Dim com As ADODB.Command
    Dim lngRetVal As Long
    
    On Error GoTo ErrorHandler
        'If Not Validate Then Exit Sub
        If strItemID = Empty Then
            strSql = "Insert Into tblItemList (ItemID,Item,CategoryID,Quantity,Rate,Half,HalfQuantity,HalfRate,ExpiryDate) Values ('" & _
            ReplaceQuote(txtItemID.Text) & "','" & ReplaceQuote(txtItem.Text) & "'," & SetDataCombo(cboCategory) & ",'" & _
            ReplaceQuote(txtQuantity.Text) & "'," & Val(txtRate.Text) & "," & IIf(chkHalfAvailable.Value, 1, 0) & ",'" & _
            ReplaceQuote(txtHalfQuantity.Text) & "'," & Val(txtHalfRate.Text) & ",?)"
        Else
            strSql = "Update tblItemList Set ItemID='" & ReplaceQuote(txtItemID.Text) & "',Item='" & ReplaceQuote(txtItem.Text) & "'," & _
            "CategoryID=" & SetDataCombo(cboCategory) & ",Quantity='" & ReplaceQuote(txtQuantity.Text) & "',Rate=" & _
            Val(txtRate.Text) & ",Half=" & IIf(chkHalfAvailable.Value, 1, 0) & ",HalfQuantity='" & ReplaceQuote(txtHalfQuantity.Text) & "'," & _
            "HalfRate=" & Val(txtHalfRate.Text) & ",ExpiryDate=?" & " Where ItemID='" & strItemID & "'"
        End If
        
        Set com = CreateCommand(strSql)
        com.Parameters.Append com.CreateParameter("ExpiryDate", adDate, adParamInput)
        If Not IsNull(dtpExpiry.Value) Then
            com("ExpiryDate") = CDate(dtpExpiry.Value)
        Else
            'Using Nothing instead of Null in the following line generates an error- ‘Application uses a value of the wrong type for the current operation’.
            com("ExpiryDate") = Null
        End If
        
        com.Execute lngRetVal, adCmdText, adExecuteNoRecords
        If lngRetVal = 1 Then
            Msgbox "Record saved."
            Unload Me
        End If
        CloseConnection

        strSql = Empty
    Exit Sub

ErrorHandler:
    CloseConnection
    strSql = Empty
    MsgBox "The following error has occurred:" & vbCrLf & Err.Description, vbCritical, "Error"
End Sub

Private Sub OpenConnection()
    If con Is Nothing Then
        Set con = New ADODB.Connection
        con.Open "Provider=SQLOLEDB.1;Data Source=MyServer;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB"
    End If
End Sub

Private Sub CloseConnection()
    If Not con Is Nothing And con.State = adStateOpen Then
        con.Close
        Set con = Nothing
    End If
End Sub

Private Function CreateCommand(strQuery As String) As ADODB.Command
    Dim com As New ADODB.Command
    
    OpenConnection
    com.ActiveConnection = con
    com.CommandText = strQuery
    Set CreateCommand = com
End Function


Now about the problem. When I try to save, the system throws an error- 'The conversion from datetime data type to smalldatetime data type resulted in a smalldatetime overflow error'.

When I change the database table column data type to datetime and try to save the data from the form, it saves the default datetime value i.e. 31/12/1899 12:00:00 AM irrespective of what I pass through the parameter. Please help me sort out this problem, preferrably with the smalldatetime data type instead of the datetime as my application mainly deals with it. Confused | :confused:
AnswerRe: Vb6 sybntax counterpart of VB.Net. Pin
Eddy Vluggen17-Jun-10 22:46
professionalEddy Vluggen17-Jun-10 22:46 
QuestionHelp with a code I can't seem to get work Pin
John McMahon11-Jun-10 7:02
John McMahon11-Jun-10 7:02 
AnswerRe: Help with a code I can't seem to get work Pin
Kschuler11-Jun-10 7:29
Kschuler11-Jun-10 7:29 
GeneralRe: Help with a code I can't seem to get work Pin
John McMahon11-Jun-10 7:31
John McMahon11-Jun-10 7:31 
GeneralRe: Help with a code I can't seem to get work Pin
John McMahon11-Jun-10 7:33
John McMahon11-Jun-10 7:33 
GeneralRe: Help with a code I can't seem to get work Pin
Wes Aday11-Jun-10 7:41
professionalWes Aday11-Jun-10 7:41 
GeneralRe: Help with a code I can't seem to get work Pin
John McMahon11-Jun-10 7:42
John McMahon11-Jun-10 7:42 
QuestionStop formatting of datagridview cell [solved] Pin
chrispowell1234511-Jun-10 1:01
chrispowell1234511-Jun-10 1:01 
AnswerRe: Stop formatting of datagridview cell Pin
Wayne Gaylard11-Jun-10 1:30
professionalWayne Gaylard11-Jun-10 1:30 
GeneralRe: Stop formatting of datagridview cell Pin
chrispowell1234511-Jun-10 3:01
chrispowell1234511-Jun-10 3:01 
GeneralRe: Stop formatting of datagridview cell Pin
Wayne Gaylard11-Jun-10 3:34
professionalWayne Gaylard11-Jun-10 3:34 
GeneralRe: Stop formatting of datagridview cell Pin
chrispowell1234511-Jun-10 5:11
chrispowell1234511-Jun-10 5:11 
GeneralRe: Stop formatting of datagridview cell Pin
Luc Pattyn11-Jun-10 6:40
sitebuilderLuc Pattyn11-Jun-10 6:40 
GeneralRe: Stop formatting of datagridview cell Pin
Wayne Gaylard11-Jun-10 16:53
professionalWayne Gaylard11-Jun-10 16:53 
GeneralRe: Stop formatting of datagridview cell Pin
Luc Pattyn11-Jun-10 17:15
sitebuilderLuc Pattyn11-Jun-10 17:15 
GeneralRe: Stop formatting of datagridview cell Pin
chrispowell1234513-Jun-10 22:30
chrispowell1234513-Jun-10 22:30 
GeneralRe: Stop formatting of datagridview cell Pin
Wayne Gaylard14-Jun-10 20:22
professionalWayne Gaylard14-Jun-10 20:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.