adding records to MS access database

nick8012

Member
Joined
Sep 19, 2007
Messages
5
Programming Experience
Beginner
Hello all,
I was wondering if anyone out there would be willing to help me with my problem. All I'm trying to do is add records through text boxes to an access database by pushing a button. I am using Visual Studio.NET 2003 and access 2003.

This is the code that i have thus far:

Imports System.Data.OleDb
Public Class NewUserInfo
Inherits System.Windows.Forms.Form
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim icount As Integer
Dim str As String

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\nick\My Documents\Capstone\Users.mdb;")
cn.Open()
str = "insert into Personal (UserName, Password) values ('" & txtNewUserName.Text & "','" & txtNewUserPW.Text & "')"

cmd = New OleDbCommand(str, cn)
icount = cmd.ExecuteNonQuery()
MessageBox.Show(icount)
'displays number of records inserted

Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
cn.Close()

End Sub
End Class

The exception keeps getting thrown. Can anybody tell me why or have any suggestions?

Any help would be greatly Appreciated. Thanks
 
Syntax was off

Hello everyone,
This past weekend I figured out why the above code does not work.
I hope that this will benefit anyone who is trying to do something similar to what I have.

I found that this line:

str = "insert into Personal (UserName, Password) values ('" & txtNewUserName.Text & "','" & txtNewUserPW.Text & "')"

was generating a syntax error and was causing my catch block to be executed.

Well the reason was is that when you refer to your field names inside of the database table you have to use [] brackets around them especially when doing an "insert into" statement.

so the above line should look like this:

str = "insert into Personal ([UserName], [Password]) values ('" & txtNewUserName.Text & "','" & txtNewUserPW.Text & "')"

I did this and now my code now works exactly the way I want it to. :D
Hope this helps.
 
Um.. actually, well.. we dont write queries like that any more - see the PQ link in my signature for details
And then, when you've digested that, know that we kinda dont do data access like that any more either.. Reading the DW1 link will take you some way there seeing as youre on the old version of Visual Studio. When you upgrade to 2005 or express, read the DW2 link..
 
To cjard

TO CJARD:
No offense, but please do not reply to my posts with links to the MSDN. I know where the MSDN is and if I wanted to go there for information I would. I joined this forum to get help mostly with the syntax of vb.net and to get some quick examples. Also, when you post your links, then it makes anyone who actually might have an answer not respond because they figure that the answer lies somewhere in your posted link. I took a look at the D1 link when you first posted it and none of that helped me. So when I found the answer to my problem I thought I would post it for any one else who might be experiencing the same problem. If you do reply, reply with some code, not links to the MSDN.
Thank You
 
TO CJARD:
No offense, but please do not reply to my posts with links to the MSDN. I know where the MSDN is and if I wanted to go there for information I would.
Those data walktrhoughs are surprisignly hard to find, actually..

I joined this forum to get help mostly with the syntax of vb.net
This post was mainly about SQL, somewhat outside the mandate you just specified..

and to get some quick examples.
Indeed, and you got some nasty ones too..

Also, when you post your links, then it makes anyone who actually might have an answer not respond because they figure that the answer lies somewhere in your posted link.
And the very reason I post the links is... *drum roll* because the answer lies in the posted link

I took a look at the D1 link when you first posted it and none of that helped me.
"There is none so blind as he who will not see" - Proverbs

So when I found the answer to my problem I thought I would post it for any one else who might be experiencing the same problem.
It's very unlikely that someone else would be facing the exact same problem as you. It is also unlikely henceforth that Google will be able to read this post, work out your problem, their search and how similar they are, and direct them here. Also they may be doing their data access in the modern, correct, microsoft-recommended way (as found in the DW1 link) and will never even encounter this problem in the first place.
That's kinda the crux argument; you came here with a problem with your solution. I offered you a solution that would never encounter that problem.

If you do reply, reply with some code, not links to the MSDN.
Thank You
You might like to bear in mind that I help literally hundreds of people here. If I replied with code, I would very rapidly get tired of doing people's work for them. Would you actually believe that I even get tired of saying "read the DW2 link in my signature, sections X, Y and Z" I say it *that often*
Take a look in some of the threads I say it in; there are usually a couple of others in that thread, posting a quick fix to the shoddy problematic code - sure they are delivering the answer the OP wants, but they dont hang around very long because they very quickly get tired of fixing the same crappy problems over and over, with the same crappy solutions.

Overwhelmingly, people here seek the answers to problems of their own causing, by following poor programming methods dating from the early nineties and horrendous habits they picked up in inconsistent, awful languages like VB6. The MSDN links pointed contain excellent code examples, straight from the manufacturer and are the best source of information avaiable. They are up-to-date, relevant and transferable directly to your situation. Even the most recently published book on ADO.NET cannot be so up to date or accurate, moreso if Microsoft didnt write it.

You might additionally consider that there are many, many people who came here doing something in a way that was just plain up wrong. After reading some excellently prepared tutorials, they had a good background understanding of how stuff worked and should be done; that's the kind of education a person receives at university.

Perhaps, if you bought a brand new computer, and were wondering how to switch it on, you'd call your mate's grandfather, because he used to work in a factory manufacturing switches before the war. Personally, I would direct you to the manual that came with the unit.

If this analogy seems a little stretched, perhaps if you came to me asking what a word meant, I would show you how to use a dictionary, rather than just straight up tell you what it meant. You'll forget what it means, and you will only remember that I know what it means so you'll approach me again for the definition later; this fact recital is the kind of education that a pre-college student receives.

If you do want exact answers to very specific problems, I'd recommend Experts Exchange; however it came about, the community there cares about solving only your exact problem and getting their points.. Not educating you so you dont need them again.

Here's hoping you stick around. Enjoy DW1! :)
 
Back
Top