Results 1 to 12 of 12

Thread: vb to sql server

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    vb to sql server

    Folks,

    I need to know why my code below locks up on the line

    rst.Open sourcesqlstatement, cnn, adOpenStatic, adLockOptimistic

    and the program says "Not Responding" in the task bar. Please help!

    My boss is in another state and needs me to send him afix withinan hour. Heellp!!!!

    Thank you,
    Jim

    Dim rst As New ADODB.Recordset
    Set cnn = CreateObject("ADODB.Connection")
    cnn.ConnectionString = "Driver={SQL Server};Server=SQL;Database=" & Trim(lblDatabase.Caption) & ";Uid=sa;Pwd=;"

    cnn.Open

    sourcesqlstatement = "Select PartName from Part where PartName <> ''"

    rst.Open sourcesqlstatement, cnn, adOpenStatic, adLockOptimistic

    If rst.RecordCount > 0 Then
    rst.MoveFirst
    While Not rst.EOF
    Value = rst.Fields("PartName")
    If Value = partname Then
    dbAdd = False
    End If
    rst.MoveNext
    Wend
    End If
    rst.Close

  2. #2
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: vb to sql server

    sourcesqlstatement = "Select PartName from Part where PartName IS NOT NULL"

    Jon

  3. #3
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    first of all, i hope it's not your boss trying to use this program from wherever he is... logging in over the internet with username=sa and no password is a huge security breach.

    but anyways, sounds like your connection works and it's just the recordset that doesnt get populated. question: how many records do you have in the database? it may be that there are too many, and the server times out...
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  4. #4
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    Originally posted by radum
    question: how many records do you have in the database? it may be that there are too many, and the server times out...
    disregard this... you would get a timeout error eventually...
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  5. #5
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: vb to sql server

    If you're using SQL Server..why are you using dynamic sql right in the front end. This should be processed on the server via a stored procedure. Your front end code should be:
    VB Code:
    1. Dim objCmd As ADODB.Command
    2.  
    3. Call EstablishConnection
    4. Set objCmd = New ADODB.Command
    5. objConn.CursorLocation = adUseClient
    6.  
    7.     With objCmd
    8.         .ActiveConnection = objConn
    9.         .CommandText = "your_sproc_name"       'our stored procedure
    10.         .CommandType = adCmdStoredProc         'its a stored procedure
    11.         Set objRS = .Execute
    12.         Set objRS.ActiveConnection = Nothing
    13.         Call ReleaseConnection
    14.     End With
    15.    
    16.     If objRS.BOF Then
    17.         'no records?
    18.        
    19.         MsgBox "Your search returned no results!",   vbExclamation, "Record Not Found!"
    20.  
    21.     Else
    22.         If objRS.EOF Then
    23.             MsgBox "Your search returned no results!", vbExclamation, "Record Not Found!"
    24.             Unload frmMain
    25.         Else
    26.               While Not objRS.EOF
    27.                          Value = objRS.Fields("PartName")
    28.                                    If Value = partname Then
    29.                                              dbAdd = False
    30.                                    End If
    31.                          objRS.movenext
    32.         End If
    33.     End If
    34.  
    35. Done:
    36. Set objCmd = Nothing
    37. Exit Sub
    38.  
    39. Err_Handler:
    40. MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
    41. Resume Done

    Your sproc should look like this:

    Code:
    CREATE PROCEDURE sproc_name_here
    AS
    BEGIN
    SET NOCOUNT ON
    Select PartName from Part where PartName IS NOT NULL
    Set NOCOUNT OFF
    End
    GO
    Later,Jon

  6. #6
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    try this piece of code (it only attempts to select 10 records and skips past the test)
    VB Code:
    1. Dim rst As New ADODB.Recordset
    2. Set cnn = CreateObject("ADODB.Connection")
    3. cnn.ConnectionString = "Driver={SQL Server};Server=SQL;Database=" & Trim(lblDatabase.Caption) & ";Uid=sa;Pwd=;"
    4.  
    5. cnn.Open
    6.  
    7. sourcesqlstatement = "Select TOP 10 PartName from Part where PartName <> ''"
    8.  
    9. On Error Resume Next
    10. rst.Open sourcesqlstatement, cnn, adOpenStatic, adLockOptimistic
    11. If Err Then
    12.   Debug.Print "Error " & CStr(Err.Number) & ": " & Err.Description
    13. Else
    14.   rst.MoveFirst
    15.   While Not rst.EOF
    16.     Value = rst.Fields("PartName")
    17.     If Value = partname Then
    18.       dbAdd = False
    19.     End If
    20.     rst.MoveNext
    21.   Wend
    22.   rst.Close
    23. End If
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  7. #7
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    oh jeez... just figured... what youre trying to do here is to set dbAdd = False if you already have a given partname in the database... it's actually a lot easier than this, you dont have to go thru all the table sequentially to accomplish that:
    VB Code:
    1. Dim rst As New ADODB.Recordset
    2. Set cnn = CreateObject("ADODB.Connection")
    3. cnn.ConnectionString = "Driver={SQL Server};Server=SQL;Database=" & Trim(lblDatabase.Caption) & ";Uid=sa;Pwd=;"
    4. cnn.Open
    5.  
    6. sourcesqlstatement = "SELECT COUNT(PartName) AS isPartThere FROM Part WHERE PartName = '" & partname & "'"
    7. rst.Open sourcesqlstatement, cnn, adOpenStatic, adLockOptimistic
    8. dbAdd = rst("isPartThere") = 0
    9. rst.Close
    and no, you dont *have to* use a stored procedure for this... it's an option, but it wont make things faster.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    records in db

    There's 31 records in the Part database. When I do a MsgBox to indicate the record count, the MsgBox is blank. Its like the table is not even being read.

    Why would there not be 31 parts counted. And then it stays in this loop indifinitely and stops responding.


    If rst.RecordCount > 0 Then
    rst.MoveFirst
    While Not rst.EOF
    Value = rst.Fields("PartName")
    If Value = partname Then
    dbAdd = False
    End If
    rst.MoveNext
    Wend
    End If
    rst.Close

    What is so strange is that I have no problems at all with this code when putting the data into Access.

    Please help. My reputation, what's left of it, is on the line!

    Jim

  9. #9
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    the .RecordCount property is not set unless you go to the last record in the recordset. i'm affraid that nothing gets actually executed...
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Originally posted by radum
    the .RecordCount property is not set unless you go to the last record in the recordset. i'm affraid that nothing gets actually executed...
    It does seem like nothing is getting executed. What do you think I should do about it, based on your looking at my code? Anybody?
    Jim

  11. #11
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    run it step by step in debug mode. put a breakpoint on the rst.Open statement and see if that thing ever returns the control to your application. check the Err object after that. and so on... or simply try to replace your code with the code in my 4th or 5th post, the really tiny one (you wont regret it afterwards, especially when the database will grow bigger).

    the infinite loop effect that youre getting may be caused by another part of your code, because what you posted doesnt seem to have a problem. worst thing that can happen if the server doesnt return a result set (which is something that i doubt) is a timeout error.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  12. #12
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    what's wrong with the solution I posted?

    Jon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width