Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a pointer on database in VB6 1

Status
Not open for further replies.

ChrisMacPherson

Programmer
Jul 3, 2000
157
GB
This may be a huge question, but can anyone give me a basic description of what I need to do, to incorporate a database in to an app. <br><br>ie.<br>How do I set up the database(which tool to use?) <br>How do I retrieve data from it.<br><br>I think SQL and one of the datagrid tools are what I'm looking for, but then again they may not be!!<br><br>Thanks for any help<br><br>Chris MacPherson
 
It really depends what you want to accomplish. <br><br>If you just want to display data from a database on a small scale then you best bet is as you said to use a datagrid combined with a data control. <br><br>If however you want more complex functionality from your database there are many other methods you can try.<br><br><br>there are a lot of examples within the MSDN help files in VB. If you run a search under ADO you will find detailed descriptions of how to connect to a DB and how to retrieve and edit data. <br><br>If you have any specific questions or requirements I will be happy to answer them or to provide you with an example of a particular method <br><br>Good Luck <br><br>
 
Vince,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thanks for the advice, I will tell you what I want to do, it's very simple! <br><br>I need the user to be able to fill in a form with details about something,say a website: url,description,rating etc.<br>They will also select a category for it to be placed in. Then this information will be placed in a database. <br><br>I want the user to be able to access the database entries, and to be able to modify them(I think modify will be the same as first part). This means sorting entries by category and displaying them. <br><br>My main wish is that the user doesn't need to have access or similar installed. I might be mis-understanding that part though (I dont know how that works**).<br><br>I have actually acheived this just by using listbox for each field in each entry, but this is probably not the way I should be doing it!! I think there may be a limit to what this can acheive and the number of entries it can handle.<br><br>**I have worked out how to read data from a .mdb file in to a databound listbox and stuff, but does that limit the application to only users with access? And how would I search through that database? SQL?<br><br>Thanks again for your time<br> <p>Chris MacPherson<br><a href=mailto:thedamager@hotmail.com>thedamager@hotmail.com</a><br><a href= on the new Browza's!!<br>
Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
Chris -<br><br>What kind of user load do you anticipate?&nbsp;&nbsp;1-5 active users can (somewhat) be handled by MS Access.&nbsp;&nbsp;Anything more than that, and you ought to be thinking about something like Sybase SQL Anywhere.&nbsp;&nbsp;More than about 25-50 users, start thinking about buying a database with some horsepower -- Oracle, MS SQL Server, Sybase, etc.<br><br>&gt; My main wish is that the user doesn't need to have access or similar installed. &lt;<br><br>If you're planning a VB project (and not a web/IIS project), each user will need the Microsoft ADO files installed on their machines.&nbsp;&nbsp;I think a lot of them get installed if they already have Internet Explorer 5, or NT Service Pack 6a (just guessing, here).<br><br>If you think you'll need more than just a datacontrol, I'd recommend you get one of the SQL books I listed in the FAQ.&nbsp;&nbsp;Probably the last one listed, as you'll need the info on creating SELECT statements (don't worry, it's easy, really!).<br><br>Chip H.<br>
 
I am not thinking about putting this app on the web, it will just be a small stand-alone application that runs on it's own. There will only therefore be one user. I dont know enough(anything!!) about networks and how they run. <br><br>I'm pretty confused when it comes to the having access installed bit. You say (I think)that to run a VB app, the user must have the microsoft ADO files installed. Does this mean that their computer will then understand what a .mdb file is? So then it would not matter if I used a .mdb file. Do you think that I'm worrying about nothing here.<br><br>How do I make sure that they have the ADO files installed? Or is that another kettle of fish?<br>&nbsp;&nbsp;<br>&nbsp;I'm just concentrating on how to construct and manipulate a simple database that has between say 1 - 500 records, each with around 5 or 6 fields.<br><br>Also - I'm a full-time student and so cannot really afford to buy books that are so expensive. The prices you quote in dollars are pretty similar to the prices it costs here(U.K) even though £1 = $1.5(roughly). Do you know of any websites,tutorials(apart from the MSDN library) that may cover these topics? <p>Chris MacPherson<br><a href=mailto:thedamager@hotmail.com>thedamager@hotmail.com</a><br><a href= on the new Browza's!!<br>
Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
<br>If the requirements that you specified above are all that there are then maybe the following code will sort it for you <br><br><br><br><b><font color=blue>Option Explicit</font></b><br><br><font color=green>'This example allows you to Add,Edit and Delete Records from the database table<br>'It also allows you to sort the records by the use of the combobox control on screen<br>'The code demonstrates the use of both a bound control and a dynamically populated control using SQL<br><br>'To use this example you must<br><br>'Have Visual basic 6.0 preferably (Service Pack 3, Freely Downloadable From Microsoft Site)<br>'NorthWind Database<br><br><br>'1. Create a Form<br>'2. Place the following controls on screen<br><br>'&nbsp;&nbsp;&nbsp;CONTROL TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONTROL NAME<br>'&nbsp;&nbsp;&nbsp;Microsoft Datagrid control 6.0 SP3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DBGrid1<br>'&nbsp;&nbsp;&nbsp;Microsoft ADO Data Control 6.0 SP3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Adodc1<br>'&nbsp;&nbsp;&nbsp;Command Button&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CmdRefresh<br>'&nbsp;&nbsp;&nbsp;ComboBox&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CmbOrder<br><br><br>'You must set the following properties<br><br>' ADO DataControl<br><br>'&nbsp;&nbsp;&nbsp;Connection String: Select Build, Microsoft Jet 4.0 OleDB Provider<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Browse to the location of the Nwind.mdb and Select it<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test the connection, Select OK if Connection Works<br><br>'&nbsp;&nbsp;&nbsp;RecordSource:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Select AdCmdTable as the Command Type<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Select A Table From The Drop DownList<br><br><br>' DataGrid Control<br><br>'&nbsp;&nbsp;&nbsp;DataSource:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Select ADODC1 From the drop down list<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(It should be the only item in the list)<br><br>'&nbsp;&nbsp;&nbsp;AllowUpdate:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set this to true if you require the<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user to have edit privilages<br><br>'&nbsp;&nbsp;&nbsp;AllowAddNew:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set this to true if you require the<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user to have Insert privilages<br><br>'&nbsp;&nbsp;&nbsp;AllowDelete:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set this to true if you require the<br>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user to have edit privilages<br><br><br>'To Check that the Connection And All of the controls are configured correctly<br>'Right Click on the datagrid and Select Retrieve Fields. If the column Headers appear<br>'in the grid then all is well<br><br>'To use this method with a non-local Access Database you will require a Mapped Drive<br>'or a correct UNC (\\Machine Name\FullPath\Test.mdb)<br>'You can get these through the connection wizard on the data control by Looking in<br>'your network neighbourhood </font><br><br><b><font color=blue>Private Sub</b></font> cmdRefresh_Click()<br><b><font color=blue>Dim</b></font> strSQL <b><font color=blue>As String</b></font><br><br><b><font color=blue>If</b></font> cmbOrder.Text &lt;&gt; &quot;&quot; <b><font color=blue>Then</b></font><br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=Green>'Check that something is in the combobox</font><br>&nbsp;&nbsp;&nbsp;&nbsp;Adodc1.CommandType = adCmdText<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=green>'Change the command type of the ADO data control<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'The type set oigionally was ADOCmdTable this only allow the return of<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'complete tables chaging it to adCmdText allows you to execute SQL against it</font><br>&nbsp;&nbsp;&nbsp;&nbsp;strSQL = &quot;Select * From Employees Order By &quot; & cmbOrder.Text & &quot; Asc&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=green>'Create the SQL from the combobox entry</font><br>&nbsp;&nbsp;&nbsp;&nbsp;Adodc1.RecordSource = strSQL<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=green>'Make the new SQL the recordsource of the Datacontrol</font><br>&nbsp;&nbsp;&nbsp;&nbsp;Adodc1.Refresh<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=green>'Refresh the control</font><br>&nbsp;&nbsp;&nbsp;&nbsp;DbGrid1.Refresh<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=green>'Refresh the grid<br>&nbsp;&nbsp;&nbsp;&nbsp;'You must do both to display the changes</font><br><b><font color=blue>End If</b></font><br><br><br><b><font color=blue>End Sub</b></font><br><br><b><font color=blue>Private Sub</b></font> Form_Load()<br><b><font color=blue>Dim</b></font> x <b><font color=blue>As Integer</b></font><br><br><br><b><font color=blue>For</b></font> x = 0 <b><font color=blue>To</b></font> DbGrid1.Columns.Count - 1<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=green>'Loop through the columns collection and place the name of each into<br>&nbsp;&nbsp;&nbsp;&nbsp;'The combobox for user selection</font><br>&nbsp;&nbsp;&nbsp;&nbsp;cmbOrder.AddItem DbGrid1.Columns(x).DataField<br><font color=green>Next</font> x<br><br><b><font color=blue>End Sub</b></font>
 
Vince,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thanks a lot for this code!! I will be testing it out today or tommorrow and will get back to you on how it works <br>and if it is what I need. It sounds like it will be though.<br><br>Thanks again!!! <p>Chris MacPherson<br><a href=mailto:thedamager@hotmail.com>thedamager@hotmail.com</a><br><a href= on the new Browza's!!<br>
Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
&gt; How do I make sure that they have the ADO files installed? Or is that another kettle of fish? &lt;<br><br>Installation of programs (at least in a production environment) is indeed another kettle of fish.&nbsp;&nbsp;For a one-off program, you can handle this manually.&nbsp;&nbsp;Go to <A HREF=" TARGET="_new"> and click the &quot;downloads&quot; link.&nbsp;&nbsp;You can then download the MDAC (MS Data Access something-that-starts-with-the-letter-'c'-this-week) and install it.&nbsp;&nbsp;It's a big file(s), but it should have everything you need.<br><br>&gt; The prices you quote in dollars are pretty similar to the prices it costs here(U.K) even though £1 = $1.5(roughly) &lt;<br><br>Ooops, didn't know you were in the UK.&nbsp;&nbsp;Book prices there are a bit steep, aren't they?&nbsp;&nbsp;I'm always amazed at the price of magazines in the train stations.&nbsp;&nbsp;I don't know of any better site to learn this stuff than hanging around here at Tek-Tips and asking lots of questions.&nbsp;&nbsp;I think that Doug & Dave (the guys who are Tecumseh Group) are working on having file archives added, but they're a ways away from having that up & running.<br><br>Chip H.<br>
 
Hello again!!<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You are probably right Chip, I have learnt so much from people here, that it's become an addiction to check out all the latest threads!! <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vince, thanks, that code works fine and shows me how to connect the databse, and I think add and delete records. But, I only want the database to be in the background(not visible) and I want to read data from the database in to seperate boxes so that I can arrange them around the interface. I think (from your code) that I can read data( and maybe add data) using the statements like 'DBGrid1.Columns(x).DataField' to navigate the grid. I think that was what I was really asking as well in the beginning. (It's hard to ask the right questions!!)<br><br>So, let me try and see if I have got this right. <br>I understand now how to connect the database to the grid.<br><br>If I want to read the data from column 3, row 4, can I write: <br><br>variablename = DBGrid1.columns(3).rows(4).text/value (I'm not sure on that bit)<br><br>Will I be able to set it that way as well?<br><br>Again thanks for your time<br>&nbsp;&nbsp; <p>Chris MacPherson<br><a href=mailto:thedamager@hotmail.com>thedamager@hotmail.com</a><br><a href= on the new Browza's!!<br>
Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
I have just been trying these things out and have worked out how to put text in to and read it out of any cell. I also came accross two methods(I think) to do with AddNew, which I guess is when you add a new record. Can you possibly tell me if this is about adding a new record because I cant find an actual AddNew statement to add another record and I dont know what format the data would have to be in to add it? i.e in a string or in six different strings, one for each field(I'm a bit lost, but starting to get the idea)<br><br>Thanks a lot.<br><br>I will be away for a week so I wont be able to test it out for that time! <p>Chris MacPherson<br><a href=mailto:thedamager@hotmail.com>thedamager@hotmail.com</a><br><a href= on the new Browza's!!<br>
Learn/t/ing D\HTML, Javascript, Java, VB5-6, COBOL, Pascal
 
The add new method works as follows <br><br>'Create a recordset <br>dim rs as recordset<br><br>'set the recordset = to the datasource recordset <br><br>set rs = adodc1.recordset <br><br>'to add a new record<br><br>rs.addnew<br><br>'This must be done for each field <br>rs!Fieldname = text1.text <br>'You must ensure that the data in the textbox is the 'correct data type for that field <br>'You can do this by using the conversion method supplied <br>'by VB eg to convert it to an integer use the folloing <br>' Cint(text1.text)<br><br>'Another thing that I should have mentioned before id that 'if you are using textboxes you can ibd them to the data 'control so that you don't have to have the grid.<br><br>'You should also look into how to create a connection 'through code <br><br>Dim cn as ADODB.connection <br><br>set cn = new ADODB.connection <br><br>&nbsp;&nbsp;&nbsp;with cn <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.connectionstring = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb;Persist Security Info=False&quot;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.open(Open the connection)<br><br>if cn.state = adstateopen then <br>&nbsp;&nbsp;&nbsp;msgbox&quot;Connected&quot;<br>else <br>&nbsp;&nbsp;&nbsp;msgbox&quot;Could not connect&quot;<br>end if <br><br><br>once you have created a connection through code you can attach any number of recordsets to it <br><br>dim rs as ADODB.recordset<br><br>set rs = new ADODB.recordset <br><br>&nbsp;&nbsp;&nbsp;with rs <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ActiveConnection = cn<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.cursortype = AdOpenDynamic <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.source = &quot;Select * From Employees&quot; (Place any SQL in Here)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.open<br>&nbsp;&nbsp;&nbsp;end with <br><br>You can then assign individual members of the recordset to other controls <br><br>text1.text = rs!FirstName<br>text2.text = rs!Lastname<br><br>You can move through the recordset in the following way <br><br>rs.movenext <br><br>check for the end of the recordset by using <br><br>if rs.eof then <br>&nbsp;&nbsp;&nbsp;&nbsp;msgbox &quot;There are no more records&quot;<br>end if <br><br>If you are going to use this a lot then I would suggest that you get a book on the use of ADO <br>WROX press havea book called ADO 2.1 which is very good.<br><br>hope this helps <br><br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;end with <br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top