473,382 Members | 1,225 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,382 software developers and data experts.

Using SQL Statements in Modules

PM
Hello, I am a beginner to VB.NET.

Here is my question:

I currently have my SQL INSERT and UPDATE functions in my form. I'm trying
to clean up the code on my form, and use modules for database functions if
possible.

The problem is - When I cut and paste the SQL code into the module, the
references to my text boxes are no longer recognized. For ex:

UPDATE tbl1 SET Field1 = txtSomeField.text

txtSomeField.text is obviously not recognized, because this code is not in
the form.

Can someone suggest the best way to overcome this, so I can use modules for
my SQL statements?

Thanks
P
Nov 21 '05 #1
4 1208
Seperating your database access from your presentation code is a good
thing. :-)

What I personally like to do is abstract all of my database
functionality to a class. Instead of putting the actual SQL in the
underlying code, though, I use stored procedures. That way, any
changes to the SQL occurs on the SQL Server, and doesn't require a
recompile.

Then, each stored procedure is accessible by a function call in the
class. Each function returns a DataTable and accepts parameters that
are used to populate the command object that I use to access the data.

It isn't a perfect solution, since it is possible for the function
signature to change if the inputs to the stored procedure changes, but
most of the time, changes that I find need to be made are usually
sandboxed to the stored procedure. (You can have the function
signature accept an array of Parameter objects so that you don't need
to change the class, but you lose some ease of use, IMHO).

So, for your example, assume that you have a stored procedure to handle
the updates and it is in your new data access class. Your function
signature may be as simple as:

Public Function UpdateSomeField(ByVal NewValue As String) As DataTable

Just a little side note, here. I opt to return a DataTable on Updates
as well, just for consistancy. You can do it however you want to, that
is just the way I like to do it. If there is an exception, I will just
rethrow it from the function.

If your class is instantiated as MyDBAccess, your code might look like:

Try

MyDBAccess.UpdateSomeField(txtSomeField.text)

Catch ex as SqlException

'// Handle exception here.

End Try

HTH,

Joseph

Nov 21 '05 #2
PM,

Using modules is definitly not the best method anymore in VB.Net.

You can try this simple walkthrough I now created for you.

It is easy however shows a lot.

Open a new windows form project

Go to the solution exlorer and select with the right click Add New and than
Add Component, normally you should rename it however don't

Try to drag a xxxdataadapterwizard on it, that does not go because it is a
wizard and not a control or whatever.
Select a database and database tabel in that and create in that wizard the
most simple "select statementd" and do for the rest everything default

xxx stand here for OleDb, Sql or Oracle

On that component form you will see than two icons.

If you rightclick the DataAdapter than you can select a to generate dataset.

Click on than and do for the rest everything default.

Now you go back to the form and drag and drop a datagrid on that.

Click somewhere outside that datagrid on your form.

In the code above the automatic by the click created Form1_Load event you
type
Public dba As new Component1

In the form event you type
dim ds as New Dataset1
dba.xxxDataadapter1.Fill(ds)
DataGrid1.DataSoure = ds.Tables(0)

Click than on debug start.

A windows form should be should (as we both did not make typos or other
errors).

This is in my idea the most simple format, however what you need is all
build for you.

If you have any questions while doing this, just reply

If it did help you than please as well, than we can set it on our website
and spare with that me typing time.

:-)

I hope this gives some ideas,

Cor
Nov 21 '05 #3
Hi,
Can someone suggest the best way to overcome this, so I can use modules for my SQL statements?


Well it may be not the *Best* way, but...

You can make a field in your module, which holds a reference to a form
and use it from functions which return SQL strings, for example:

~
Module SQLStmts
Private m_FormToUse As Form1

Public Sub SetForm(ByVal FormToUse As Form1)
If FormToUse Is Nothing Then Throw New
ArgumentNullException("FormToUse")
m_FormToUse = FormToUse
End Sub

Public Function MyUpdateStmt() As String
If m_FormToUse Is Nothing Then Throw New
InvalidOperationException("You must provide a form first.")
Return "UPDATE tbl1 SET Field1 = " &
m_FormToUse.txtSomeField.Text
End Function
End Module
~

Alternatively, you can pass entire strings (or textboxes) in your
functions, i.e.:

~
Public Function AnotherStmt(ByVal TableName As String) As String
Return "SELECT one, two, three FROM " & TableName & " ORDER BY
two DESC"
End Function
~

Also consider using Shared String.Format() method, because you'll be
able to store strings in a single constant:

~
Const KillSomeDB As String = "DROP DATABASE {0}"

....

Dim MyCmd As String = String.Format(KillSomeDB, "MyDB")
~

I hope this helps,
Roman
Nov 21 '05 #4
If your project starts from a subroutine like "Main" then in the module where
Main is at you could declare:

Public MyNewForm as MyBaseForm

where MyBaseForm is the name you gave to your form in it's properties.

Then you could just simply refer to MyNewForm.txtSomeField.Text
"PM" wrote:
Hello, I am a beginner to VB.NET.

Here is my question:

I currently have my SQL INSERT and UPDATE functions in my form. I'm trying
to clean up the code on my form, and use modules for database functions if
possible.

The problem is - When I cut and paste the SQL code into the module, the
references to my text boxes are no longer recognized. For ex:

UPDATE tbl1 SET Field1 = txtSomeField.text

txtSomeField.text is obviously not recognized, because this code is not in
the form.

Can someone suggest the best way to overcome this, so I can use modules for
my SQL statements?

Thanks
P

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shane Hathaway | last post by:
Let's talk about the problem I really want help with. I brought up a proposal earlier, but it was only half serious. I realize Python is too sacred to accept such a heretical change. ;-) ...
1
by: Ted | last post by:
I managed to get it installed OK, along side MS Visual Studio 2005 (with which I received it). During the install, I made sure I installed everything. I have developed a number of applications...
1
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
0
by: David | last post by:
- Are there any peculiarities with using curs.executemany(...) vs. multiple How many times are you calling execute vs a single executemany? The python call overhead will add up for thousands of...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.