473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

connection of SQL

hi i am using Visual Basic6.0 & SQL Server.Now i want connect SQL with Visual Basic.In SQL my one Query that is "Select creditamt from all_trlbal where clcode=101". Value of this query i want put in textbox when i click on command button of Visual Basic's form.which type i do it give some tips please.
thankyou
Feb 13 '07 #1
6 2280
dorinbogdan
839 Expert 512MB
Update the strConn with your SQL server / user information,
and replace the cmdButton and txtAmount with the correct button / textbox name.

Expand|Select|Wrap|Line Numbers
  1. Private Function GetValue() as string
  2. on error goto EH
  3. dim cn as object
  4. dim rs as object
  5. dim strSQL as string
  6.     GetValue = ""
  7.     set cn = createobject("adodb.connection")
  8.     set rs = createobject("adodb.recordset")
  9.     cn.open "Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
  10.     strSQL = "Select creditamt from all_trlbal where clcode=101"
  11.     rs.open strSQL, cn, adOpenStatic, adLockReadOnly
  12.     if not (rs.bof and rs.eof)
  13.         rs.movefirst
  14.         GetValue = val("" & rs(0));
  15.     end if
  16.     rs.close
  17.     cn.close
  18. PreExit:
  19.     set rs = nothing
  20.     set cn = nothing
  21. exit function
  22. EH:
  23.     msgbox err.number & ": " & err.description
  24.     resume PreExit
  25. end function 
  26.  
  27. private sub cmdButton_Click()
  28.    txtAmount.text = GetValue()
  29. end sub
  30.  
Feb 13 '07 #2
hi thanks
now this give me some error .Error is :- -2147467259:[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.
u send me these code in this code samoe line not work properly in this line give error Syntex Error.for this i do comment linein if condition give error. plz see it.


Expand|Select|Wrap|Line Numbers
  1. Private Function GetValue() as string
  2. on error goto EH
  3. dim cn as object
  4. dim rs as object
  5. dim strSQL as string
  6.     GetValue = ""
  7.     set cn = createobject("adodb.connection")
  8.     set rs = createobject("adodb.recordset")
  9.     cn.open "Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
  10.     strSQL = "Select creditamt from all_trlbal where clcode=101"
  11.     rs.open strSQL, cn, adOpenStatic, adLockReadOnly
  12.     'if not (rs.bof and rs.eof)
  13.     '    rs.movefirst
  14.     '    GetValue = val("" & rs(0));
  15.     'end if
  16.     rs.close
  17.     cn.close
  18. PreExit:
  19.     set rs = nothing
  20.     set cn = nothing
  21. exit function
  22. EH:
  23.     msgbox err.number & ": " & err.description
  24.     resume PreExit
  25. end function 
  26.  
  27. private sub cmdButton_Click()
  28.    txtAmount.text = GetValue()
  29. end sub
  30.  
[/quote]
Feb 15 '07 #3
dorinbogdan
839 Expert 512MB
Ok,
You must replace
"myServer" with your SQL Server name or IP,
"myDataBase" with your Database
"myUsername" with your SQL user
"myPassword" with your SQL password

I fixed the syntax error.
(I don't have VB6 installed)

Please let me know.

Expand|Select|Wrap|Line Numbers
  1. Private Function GetValue() as string
  2. on error goto EH
  3. dim cn as object
  4. dim rs as object
  5. dim strSQL as string
  6.     GetValue = ""
  7.     set cn = createobject("adodb.connection")
  8.     set rs = createobject("adodb.recordset")
  9.     cn.open "Driver={SQL Server};Server=myServer;Database=myDataBase  ;Uid=myUsername;Pwd=myPassword;"
  10.     strSQL = "Select creditamt from all_trlbal where clcode=101"
  11.     rs.open strSQL, cn, adOpenStatic, adLockReadOnly
  12.     if not (rs.bof and rs.eof) then
  13.         rs.movefirst
  14.         GetValue = val("" & rs(0))
  15.     end if
  16.     rs.close
  17.     cn.close
  18. PreExit:
  19.     set rs = nothing
  20.     set cn = nothing
  21. exit function
  22. EH:
  23.     msgbox err.number & ": " & err.description
  24.     resume PreExit
  25. end function 
  26.  
  27. private sub cmdButton_Click()
  28.    txtAmount.text = GetValue()
  29. end sub
  30.  
Feb 15 '07 #4
i give every thing in these code like as server name, database name, user name but balank password field bcoz here do not give password in SQL server.now i run project then give error Login failed for user 'sa' plz check it.code is following :-



.

Expand|Select|Wrap|Line Numbers
  1. Private Function GetValue() as string
  2. on error goto EH
  3. dim cn as object
  4. dim rs as object
  5. dim strSQL as string
  6.     GetValue = ""
  7.     set cn = createobject("adodb.connection")
  8.     set rs = createobject("adodb.recordset")
  9.     cn.open "Driver={SQL Server};Server=BITSOFT1;Database=Newdemat;Uid=sa;Pwd=   ;"
  10.  
  11.     strSQL = "Select creditamt from all_trlbal where clcode=101"
  12.     rs.open strSQL, cn, adOpenStatic, adLockReadOnly
  13.     if not (rs.bof and rs.eof) then
  14.         rs.movefirst
  15.         GetValue = val("" & rs(0))
  16.     end if
  17.     rs.close
  18.     cn.close
  19. PreExit:
  20.     set rs = nothing
  21.     set cn = nothing
  22. exit function
  23. EH:
  24.     msgbox err.number & ": " & err.description
  25.     resume PreExit
  26. end function 
  27.  
  28. private sub cmdButton_Click()
  29.    txtAmount.text = GetValue()
  30. end sub
  31.  
[/quote]
Feb 19 '07 #5
dorinbogdan
839 Expert 512MB
If the password is blank then let the parameter empty "......Pwd=;"
Feb 19 '07 #6
ya, i am give empty parameter but give me error login failed for user 'sa' . now what do i do.give me solution Please

If the password is blank then let the parameter empty "......Pwd=;"
Feb 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: G-Fit | last post by:
Hello group, I have several servers hosting SQL databases. On each of them, I have several databases. All those databases have the same structure (even those on different servers), only the data...
11
by: pradeep_TP | last post by:
Hi all, I have a few questions that I have been wanting to ask for long. These are all related to ADO.net and specifically to conenction to database. 1) If I have opened a connection to a...
6
by: Chris Szabo | last post by:
I've created a data access layer for a .NET web application. I'm using C# and framework 1.1. I'm getting an error from time to time when I close a connection saying: ...
18
by: Rob Nicholson | last post by:
We're getting an occasional occurrence of the following error when two users try and open the same record in our ASP.NET app: "There is already an open DataReader associated with this Connection...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
3
by: fniles | last post by:
In the Windows application (using VB.NET 2005) I use connection pooling like the following: In the main form load I open a connection using a connection string that I stored in a global variable...
0
by: Robert Avery | last post by:
In VBA/VB6, I had a class (incomplete sample below) that watched and displayed for the user all connection events, so that I could easily see what SQL was taking a long time, and when it freezes, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.