Log in

View Full Version : Vba excel Inputbox



caliphus
06-04-2012, 10:38 AM
Hi, I am searching for a command that will find and display all the information about a person whose name and second name will be written in Inputbox.

For example, in the database are these:

name / second name / telephone / income / ID
bob / brown / 2999122 / 1000$ /0012415
nick / smith / 2412512 / 1000$ /0015114
john / lee / 2151251 / 1000$ /0012515

I want to write in inputbox (for example) nick smith, and want these-
- nick smith 2412512 1000$ 0015114 to be displayed separately in line I want
for example in A40 A41 A42 A43 A44


PLZ help :dunno

CodeNinja
06-14-2012, 09:38 AM
Caliphus,
Not sure how far you are along this process, so I will give an overview I hope to be helpful to you. I am assuming you are using an access database as this is posted in the access forum...

To create an ADO connection:
First thing you need to do is set your reference. Go to VB Editor and select tools/references and scroll down to and select Microsoft ActiveX Data Objects Libraray 2.0

Next, create a to subroutine that connects and grabs the data you want, you need a few elements... a connection, a connection string, and a recordset.

sub getDataFromAccess()
dim cn as ADODB.connection 'this will be your connection
dim rs as ADODB.Recordset 'this will be your recordset
dim sSql as string 'this will be your SQL string
dim sCstring as string 'this will be your connection string
dim sName as string 'this is where you will store the name

'to open the connection, you need to create it first
set cn = new adodb.connection

'you will need a connection string
cString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=[path and filename];Persist Security Info=False;"
'where it says [path and filename] you will have to replace that with the actual path of the database (IE C:/AccessDb.mdb) or whatever your database's path and name are

'Next you will need to figure out your SQL statement...
sName = inputbox("Enter the name of the person.")
sSql = "select * from [Table Name] where name = '" & sname & "';"

'Now you need to execute the statement to bring the data into your recordset.

set rs = cn.execute(sSql)

'now that the data is in the recordset, you can put it where you want...

sheet1.cells(1,1) = rs.Fields("name")
sheet1.cells(1,2) = rs.Fields("second name")
' and so on...

'lastly, clean everything up by closing them
set rs = nothing
cn.close
set cn = nothing


exit sub


I hope this has been helpful to you. Good luck.