Sign in to follow this  

Access Database- Add another row

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

(First thing- I am one of the sleeper users that rarely posts but benifits from other questions and the search, when its up :)) Anyway, I have two questions where one is rather quick and another might take time. This is in VB.Net, thanks. 1.) I have an Access Database with the columns UserID(key), Username, Password, E-mail. The startup form asks the user to supply a Username and their Userpassword in two textboxs. When the user clicks OK, I know how to look for existing Usernames and check the UserPassword, but how do you create a new row of data if there is no Username? For example: There is one client in the databse with UserID 1, Username = James14 and userpassword = gamedev. But if the user puts: UserID 2, Username = Matt20, userpassword = devgames, how you write some code that enters in that userID, username, userpassword for later referance? Another example is when you register a character for a game, you log in for the first time at level 1 automatically and it saves automatically so that when you come back the next day you dont have to start over again. 2.)Being the newb at this as I am, how do I save the database so that when I exit the client and re-open it, it doesn't lose the added information? Right now, this is mainly just for me to get more familiar with using Database's before I do anything major. Thanks alot, any help is appreciated.

Share this post


Link to post
Share on other sites
SQL does not have "insert or create", which is quite unfortunate. I have wanted that many times. Instead, you have to accomplish the same thing using transactions.

Something like:


SQL> begin transaction;
SQL> select username, userpass from users where username='foo';
C++> if( numrows == 0 ) {
SQL> insert into users(username,userpass) values('foo','bar');
C++> } else if( userpass != 'bar' ) {
C++> verification failure
C++> } else {
C++> verification success
C++> }
SQL> commit transaction;
C++> if( transaction_error ) {
C++> Oops! Didn't work. User not created, etc.
C++> }


Of course, auto-creating a new account just because someone spelled their account name wrong isn't usually what you actually want to do.

Share this post


Link to post
Share on other sites
Thanks for the reply, however right now I am coding in VB. I am not sure how to convert this to VB, i will try the best that I can do.

Quote:
Original post by hplus0603
Of course, auto-creating a new account just because someone spelled their account name wrong isn't usually what you actually want to do.


Heh, yeah they will have to go through a registering process (put in their e-mail then get the code that is sent and put it in the box through a command or something like that)

Share this post


Link to post
Share on other sites
The main take-away should be "if you want to do things without interference from other users, and make sure they commit before the connection is broken, use SQL transactions".

Exactly how to use SQL from your language of choice is up to your development environment and libraries. One popular way is using ODBC, another is (as AP says) ADO.NET.

Share this post


Link to post
Share on other sites
You might get a better answer in the begginers forum, or on a general development forum, I would suggest www.gotdotnet.com as there is a lot of VB.Net activity there for these types of questions.

But.

For this problem you would have to do a couple of commands to collect the data relating to the user then either insert or update.

I would.
Collect User information from the interface.
Then use a datareader object to read and compare the information from the database.
If there is user information there relating to the data entered by the use I would update the record.
If there was no data there. I would do some more validation then create the record using a OLDBCommand object.

Share this post


Link to post
Share on other sites

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this